Slider中没有更改Int Array中的值(itemsControl)

问题描述 投票:-2回答:1

我有一个int数组,我用它来在ItemsControl中创建一堆滑块。我在滑块上使用双向绑定,但是数组永远不会被设置(我在setter上设置了一个断点)。所有这些都在UserControl中。

UserControl XAML:

<ItemsControl ItemsSource="{Binding Values, Mode=TwoWay}">
<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal" />
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <StackPanel Width="30" MaxWidth="30">
            <Slider Orientation="Vertical" Margin="5,0,0,0"
                                                Value="{Binding Path=., Mode=TwoWay}"
                                                Maximum="100"
                                                Minimum="-100"
                                                Height="100"/>
            <TextBox Text="{Binding Path=., Mode=TwoWay}" Name="NumberTextBox" PreviewTextInput="NumberValidationTextBox"/>
        </StackPanel>
    </DataTemplate>
</ItemsControl.ItemTemplate>

UserControl Codebehind:

    public int[] Values
    {
        get { return (int[])GetValue(ValuesProperty); }
        set { SetValue(ValuesProperty, value); }
    }
    public static readonly DependencyProperty ValuesProperty =
     DependencyProperty.Register("Values", typeof(int[]), typeof(Equalizer), new UIPropertyMetadata(new int[] { 0,0 }));

UserControl在MainWindow中创建,它在其中提供了值:

        <local:Equalizer Margin="50" Height="20" Width="100" VerticalAlignment="Top"
                     MyText="{Binding TextData, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}}"
                     MyProperty="True" 
                     MinValue="{Binding MinValue, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}}"
                     MaxValue="{Binding MaxValue, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}}"
                     Values="{Binding Values, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}, Mode=TwoWay}"/>
wpf binding user-controls dependency-properties itemscontrol
1个回答
-1
投票

好的,我找到了办法。它不漂亮,但它的工作原理..

在xaml中,我绑定到一个名为“EQValues”的集合,如下所示:

                            <ItemsControl ItemsSource="{Binding EQValues, Mode=TwoWay}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Width="30" MaxWidth="30">
                                        <Slider Orientation="Vertical" Margin="5,0,0,0"
                                                Value="{Binding Value, Mode=TwoWay}"
                                                Maximum="{Binding MaxValue, Mode=TwoWay}"
                                                Minimum="{Binding MinValue, Mode=TwoWay}"
                                                Height="100"/>
                                        <TextBox Text="{Binding Value, Mode=TwoWay}" 
                                                 Name="NumberTextBox" 
                                                 PreviewTextInput="NumberValidationTextBox"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>

然后在代码隐藏中,每当int数组发生更改时,我都会将此集合更改为等于int数组:

        public int[] Values
    {
        get { return (int[])GetValue(ValuesProperty); }
        set { SetValueDp(ValuesProperty, value); }
    }
    public static readonly DependencyProperty ValuesProperty =
        DependencyProperty.Register("Values",
            typeof(int[]),
            typeof(Equalizer),
            new FrameworkPropertyMetadata(OnValuesChanged));

    public static void OnValuesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // Get this specific Equalizer and call the UpdateCollection method
        Equalizer myUserControl = (Equalizer)d;
        myUserControl.UpdateCollection(e.NewValue as int[]);
    }

    void UpdateCollection(int[] intArr)
    {
        // Set EQValues to Values-Array, but as classes so ItemsControl can display it
        EQValues = new ObservableCollection<EQItem>();
        for (int i = 0; i < Values.Length; i++)
            EQValues.Add(new EQItem(Values[i], MinValue, MaxValue));
    }

然后我通过将集合转换回int数组来保存设置数组:

        // Convert EQValues into array
        int[] arr = new int[EQValues.Count];
        for (int i = 0; i < EQValues.Count; i++)
        {
            arr[i] = EQValues[i].Value;
        }
        // Set values to new values
        Values = arr;

再次,不漂亮,但它的工作:)。

© www.soinside.com 2019 - 2024. All rights reserved.