2009-10-21 46 views
2

我創建了一個小例子來演示我遇到的問題。WPF INotifyPropertyChanged不更新數組屬性?

一是我的課:


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

的問題是價值觀的性質,因爲它是一個數組。我不確定如何在數組中的元素更新時正確調用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上。如果我更改Title屬性,則標題會正確更新。

任何想法如何我可以調用INotifyPropertyChanged,以便它瞭解數組元素已更新?

+0

This one is close .. http://stackoverflow.com/questions/90971/how-do-i-use-inotifypropertychanged-to-update-an-array-binding – Kelly 2009-10-21 16:49:18

回答

7

您現有的代碼不起作用的原因是因爲您要修改數組中的值,而不是將整個數組本身重新分配給屬性。因此你不會發射財產變更事件。

我根本不會使用數組,而是使用ObservableCollection代替它,它實現了WPF綁定基礎結構掛接的INotifyCollectionChanged,以更好地理解集合本身內的更改。

+0

正在尋找更好的方法來處理這個問題沒有ObservableCollection,但看起來這是我需要去的路線。 – Kelly 2009-10-21 19:56:23

+0

你可以添加一個示例代碼嗎? – Butzke 2014-03-07 11:09:34

2

您可以使用ObservableCollection<int?>而不是陣列來爲您完成所有管道工作。

另一種選擇可能是製作一個int?容器,用這些容器填充陣列,並在int?的制定者上點火PropertyChanged事件。

相關問題