我有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;
我将RowData
的属性与我的列的DisplayMemberBinding
属性绑定在一起,例如:
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"
。
为什么?我该如何解决这个问题?
答案 0 :(得分:3)
您忘了实施INotifyPropertyChanged
interface
更新数据类中的任何属性后,您需要通知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));
}
}
}