WPF INotifyPropertyChanged 未更新数组属性?

发布于 2024-08-08 16:38:28 字数 3271 浏览 3 评论 0原文

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

首先是我的课程:


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

问题是 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;
        }
    }
}

当我单击按钮时,它会更改第一行的值,但更改不会反映在 UI 上。如果我更改“标题”属性,标题会正确更新。

有什么想法可以调用 INotifyPropertyChanged 以便它了解数组元素已更新吗?

I created a small example to demonstrate the issue I'm having.

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, since it is an array. I'm not sure how to properly call INotifyPropertyChanged when an element in the array gets 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, yet that change is not reflected on the UI. If I change the Title property, the title updates correctly.

Any ideas how I can call INotifyPropertyChanged so that it understands an array element was updated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耀眼的星火 2024-08-15 16:38:28

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

我根本不会使用数组,使用 ObservableCollection 相反,它实现了 INotifyCollectionChanged,WPF 绑定基础结构挂钩该 INotifyCollectionChanged,以更好地了解集合本身内的更改。

The reason your existing code doesn't work is because you're modifying the value within the array, not reassigning entire array itself to the property. Therefore you won't be firing a property change event.

I wouldn't use an array at all, use an ObservableCollection instead which implements INotifyCollectionChanged which the WPF binding infrastructure hooks into to better understand changes within the collection itself.

堇色安年 2024-08-15 16:38:28

您可以使用 ObservableCollection 而不是数组来为您完成所有的工作。

另一种选择可能是创建一个 int? 容器,用这些容器填充数组,然后在该 int? 上触发 PropertyChanged 事件二传手。

You could use an ObservableCollection<int?> instead of the array, to do all the plumbing for you.

Another option could be to make an int? container, fill the array with those, and the fire the PropertyChanged event on that int?'s setter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文