Значения в массиве Int не изменяются с помощью ползунка (itemsControl) - PullRequest
0 голосов
/ 10 сентября 2018

У меня есть массив int, который я использую для создания группы ползунков в ItemsControl.Я использую двустороннюю привязку на ползунках, но массив никогда не устанавливается (я устанавливаю точку останова на установщик).Все это в 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}"/>

1 Ответ

0 голосов
/ 14 сентября 2018

Хорошо, я нашел способ. Это не красиво, но работает ..

В 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));
    }

Затем я сохраняю set-массив, преобразовывая коллекцию обратно в 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;

Опять не красиво, но работает :).

...