C# WPF INotifyPropertyChanged未更新数组属性?

C# WPF INotifyPropertyChanged未更新数组属性?,c#,wpf,inotifypropertychanged,C#,Wpf,Inotifypropertychanged,我创建了一个小示例来演示我遇到的问题 我的第一堂课: 问题在于Values属性,因为它是一个数组。我不确定在数组中的元素更新时如何正确调用INotifyPropertyChanged。 这是我的xaml: <Window x:Class="WpfApplication5.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.mi

我创建了一个小示例来演示我遇到的问题

我的第一堂课:



问题在于Values属性,因为它是一个数组。我不确定在数组中的元素更新时如何正确调用INotifyPropertyChanged。

这是我的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>

按钮
以及背后的代码:

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;
        }
    }
}
公共部分类窗口1:窗口
{
私有只读ObservableCollection displayRows=新ObservableCollection();
公共窗口1()
{
初始化组件();
Add(newdisplayRow{Title=“Item 1”,value=newint?[{1,2,3,4,5,6}});
Add(newdisplayrow{Title=“Item 2”,value=newint?[{7,8,9,10,11,12}});
Add(newdisplayrow{Title=“Item 3”,value=newint?[{13,14,15,16,17,18}});
MyListBox.ItemsSource=displayRows;
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
foreach(displayRows中的DisplayRow行)
{
行值[0]=99;
}
}
}
当我单击按钮时,它会更改第一行的值,但这种更改不会反映在UI上。如果更改“标题”属性,则标题将正确更新


您知道如何调用INotifyPropertyChanged以便它了解数组元素已更新吗?

现有代码无法工作的原因是您正在修改数组中的值,而不是将整个数组本身重新指定给属性。因此,您不会触发属性更改事件


我根本不会使用数组,而是使用一个实现INotifyCollectionChanged的数组,WPF绑定基础结构将其挂钩,以更好地理解集合本身内的更改。

您可以使用一个
observeCollection
来代替数组,为您完成所有的工作


另一个选项是创建一个
int?
容器,用这些容器填充数组,然后在该
int?
的setter上触发
PropertyChanged
事件。

此选项已关闭。。我们正在寻找一种更好的方法来处理这个问题,而不需要观察到收集,但这似乎是我需要走的路线。
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;
        }
    }
}