WPF INotifyPropertyChanged not updating array property?

I created a small example to demonstrate that I have a problem.

First my class:


public class DisplayRow : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private int?[] values;
    private string title;

    public string Title
    {
        get { return title; }
        set
        {
            title = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Title"));
        }
    }

    public int?[] Values
    {
        get { return values; }
        set
        {
            values = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Values[]"));
        }
    }

    public DisplayRow()
    {
        Values = new int?[6];
    }
}

      


The problem is the Values โ€‹โ€‹property as it is an array. I'm not sure how to properly call INotifyPropertyChanged when an array element is updated.

Here is my xaml:

<Window x:Class="WpfApplication5.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <ListBox x:Name="MyListBox" Margin="0,0,0,65">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="{Binding Path=Title}" />
                        <TextBlock Text="{Binding Path=Values[0]}" Margin="5,0,0,0" />
                        <TextBlock Text="{Binding Path=Values[1]}" Margin="5,0,0,0"  />
                        <TextBlock Text="{Binding Path=Values[2]}" Margin="5,0,0,0"  />
                        <TextBlock Text="{Binding Path=Values[3]}" Margin="5,0,0,0"  />
                        <TextBlock Text="{Binding Path=Values[4]}" Margin="5,0,0,0"  />
                        <TextBlock Text="{Binding Path=Values[5]}" Margin="5,0,0,0"  />                        
                    </WrapPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Height="23" Margin="27,0,0,23" Name="button1" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="74" Click="button1_Click">Button</Button>
    </Grid>
</Window>

      

And the code behind:

public partial class Window1 : Window
{
    private readonly ObservableCollection<DisplayRow> displayRows = new ObservableCollection<DisplayRow>();

    public Window1()
    {
        InitializeComponent();

        displayRows.Add(new DisplayRow {Title = "Item 1", Values = new int?[] {1, 2, 3, 4, 5, 6}});
        displayRows.Add(new DisplayRow {Title = "Item 2", Values = new int?[] {7, 8, 9, 10, 11, 12}});
        displayRows.Add(new DisplayRow {Title = "Item 3", Values = new int?[] {13, 14, 15, 16, 17, 18}});

        MyListBox.ItemsSource = displayRows;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        foreach (DisplayRow row in displayRows)
        {
            row.Values[0] = 99;
        }
    }
}

      

When I click on the button, it changes the values โ€‹โ€‹of the first row, but this change is not reflected in the user interface. If I change the Title property, the title updates correctly.

Any ideas how I can call INotifyPropertyChanged so that it understands the array item has been updated?

+2


source to share


2 answers


The reason your existing code is not working is because you are changing the value in the array rather than reassigning the entire array in the property. Therefore, you will not fire a property change event.



I would not use an array at all, instead of the ObservableCollection , which implements INotifyCollectionChanged, which the WPF binding framework is bound to, in order to better understand the changes within the collection itself.

+7


source


Instead of an array, you can use ObservableCollection<int?>

to do all the plumbing for you.



Another option would be to create a container int?

, populate an array with these objects, and fire an event PropertyChanged

for that installer int?

.

+2


source







All Articles