-2

I have an int array, which I'm using to create a bunch of sliders in an ItemsControl. I'm using a two-way binding on the sliders, but the array never gets set (I put a breakpoint on the setter). All of this is in a 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 }));

The UserControl is created in MainWindow, where it's fed the Values:

        <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}"/>
Hjalte Tagmose
  • 67
  • 1
  • 12
  • 1
    This won't work. You need an item class with an int property, and bind your ItemsControl to a collection of the item class. – Clemens Sep 10 '18 at 17:32
  • The application is supposed to get data in from the outside and the values are in an int array. This is non-negotiable. I tried to change it into an observablecollection and back with a converter, but it still wont do 2-way binding. – Hjalte Tagmose Sep 13 '18 at 13:02
  • 1
    Oh, I am not going to negotiate that. Just saying it won't work. – Clemens Sep 13 '18 at 16:30
  • 1
    but if that int array does not support INotifyPropertyChanged then two-way binding is not going to work. – Jeff R. Sep 13 '18 at 19:14
  • I meant in my task, the data is gonna be int array, cant change that, but I did figure out a way to do it. Thanks for you comments! :) – Hjalte Tagmose Sep 14 '18 at 06:51

1 Answers1

-1

Okay I found a way. It's not pretty, but it works..

In the xaml I bind to a collection called "EQValues" like so:

                            <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>

And then in the codebehind I change this collection to be equal to the int array, whenever the int array is changed:

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

Then I save set the array by converting the collection back into an int-array:

        // 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;

Again, not pretty, but it works :).

Hjalte Tagmose
  • 67
  • 1
  • 12