zoukankan      html  css  js  c++  java
  • WPF ListView / ListBox 更新绑定数据源时,自动刷新界面显示

    问题现象:ListView 更新绑定数据源时,界面不能自动刷新。

    原因及解决方案:

    1、ListView 的 ItemSource 使用 BindingList < T >;

     注:由于 List < T > 没有实现 INotifyPropertyChanged 接口,

       因此若使用 List < T > 作为 ItemSource,则当 ListView 新增、删除 Item 时,ListView UI 会不能即时更新;

    2、对应 ListView 的 Item 的类 T 实现 INotifyPropertyChanged 接口;

      T 中 UI 绑定对应的属性 Set 设值后,调用 PropertyChanged() 函数以通知 UI 该属性已改变,示例如下:

      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();

          BindingList<Customer> listCustomer = new BindingList<Customer>();
          listCustomer.Add(new Customer() { Name = "ZhangSan" });
          listView.ItemsSource = listCustomer;
        }
      }
      public class Customer : INotifyPropertyChanged
      {
        public string name;
        public string Name
        {
          get { return name; }
          set { name = value; OnPropertyChanged(new PropertyChangedEventArgs("Name")); }
        }

        #region // INotifyPropertyChanged成员
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
          if (PropertyChanged != null)
          {
            PropertyChanged(this, e);
          }
        }
        #endregion
      }

  • 相关阅读:
    Linux命令大全之关机重启命令
    Linux命令大全之帮助命令及压缩命令
    Linux命令大全之搜索命令
    Linux命令大全之基本命令
    Linux安装界面简介
    Linux 的ftp服务未启用怎么办
    创建分区表
    Linux分区,格式化概念理解
    VMware简介
    Linux简介
  • 原文地址:https://www.cnblogs.com/dhqy/p/8694613.html
Copyright © 2011-2022 走看看