c# - ListView 不刷新其项目

标签 c# wpf listview binding

我有一个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 属性绑定(bind),例如:

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")。 为什么?我该如何解决这个问题?

最佳答案

您忘记实现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));
     }
 }
 }

关于c# - ListView 不刷新其项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12654196/

相关文章:

c# - 一种根据另一种风格激活的风格?

c# - 异步 WCF 调用来保存线程?

android - 如何将我的动态布局添加到 ListView?

基于 SystemColors 的 WPF 通用颜色

Android ListView 计时器每秒运行一次

asp.net - 在 DataBound 事件上获取绑定(bind)到 ListView 的数据

c# - 如何在 c# .net web 应用程序中获取特定的资源字符串而不考虑当前的文化

c# - 如何在 C# 中使用屏幕分辨率缩放表单组件?

javascript - ASP.Net - 在错误页面上隐藏子菜单

c# - 系统.InvalidOperationException : 'There is an error in XML document (0, 0).'