【问题标题】:ListView does not refresh its itemsListView 不刷新其项目
【发布时间】:2012-09-21 04:13:27
【问题描述】:

我有一个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 有一个新值 - 新的双击显示 @ 987654331@ 在分配新字符串之前)。
为什么?我该如何解决这个问题?

【问题讨论】:

标签: c# wpf listview binding


【解决方案1】:

你忘了实现INotifyPropertyChangedinterface

更新数据类中的任何属性后,您需要通知 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));
     }
 }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    相关资源
    最近更新 更多