簡體   English   中英

WPF INotifyPropertyChanged 未更新數組屬性?

[英]WPF INotifyPropertyChanged not updating on an array property?

我創建了一個小示例來演示我遇到的問題。

首先我的課:


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。

這是我的xml:

<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 上。 如果我更改 Title 屬性,則標題會正確更新。

有什么想法可以調用 INotifyPropertyChanged 以便它了解數組元素已更新?

現有代碼不起作用的原因是因為您正在修改數組中的值,而不是將整個數組本身重新分配給屬性。 因此,您不會觸發屬性更改事件。

我根本不會使用數組,而是使用ObservableCollection來實現 INotifyCollectionChanged,WPF 綁定基礎結構掛鈎以更好地理解集合本身的變化。

您可以使用ObservableCollection<int?>而不是數組來為您完成所有工作。

另一種選擇可能是制作一個int? 容器,用這些填充數組,然后在該int?上觸發PropertyChanged事件? 的二傳手。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM