2

我有一个ListView地方:

public class RowData
{
    public string Box1 { get; set; }
    public string Box2 { get; set; }
    public string Box3 { get; set; }
};

private ObservableCollection<RowData> Data = new ObservableCollection<RowData>();

...

MyListView.ItemsSource = Data;

我将 的属性RowDataDisplayMemberBinding列的属性绑定,例如:

MyGridViewColumn.DisplayMemberBinding = new Binding("Box1"));

我处理ListViewItem.DoubleClick事件:

private void ListViewItem_DoubleClick(object sender, MouseButtonEventArgs e)
{
    ListViewItem item = sender as ListViewItem;
    RowData data = item.DataContext as RowData;
    data.Box1 = "new string";
}

但是,当我将新字符串分配给我的数据时,ListView 不会刷新其项目(Box1即使Box1有新值,我仍然可以看到 的旧值 - 新的双击显示Box1 == "new string"在分配新字符串之前)。
为什么?我怎么解决这个问题?

4

1 回答 1

4

你忘了实现INotifyPropertyChanged 接口

更新数据类中的任何属性后,您需要通知 View 自行更新。

public class RowData : INotifyPropertyChanged
{
    private string box1;
    public string Box1 
    {
        get { return box1; }
        set
        {
            if(box1 == value) return;
            box1= value;
            NotifyPropertyChanged("Box1 ");
        }
    }
 //repete the same to Box2 and Box3

 public event PropertyChangedEventHandler PropertyChanged;

 private void NotifyPropertyChanged(String propertyName)
 {
     if (PropertyChanged != null)
     {
         PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     }
 }
 }
于 2012-09-29T16:10:13.847 回答