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
      }

  • 相关阅读:
    FTP命令行工具NCFTP
    XP 通过无线网卡 建立对等网
    Silverlight WCF 压缩
    EntityFramework Linq查询
    UCS2编码转换C#
    C#7Z压缩
    c#公钥加密私钥解密和验证
    SVN global ignore pattern for c#
    典型的DIV CSS三行二列居中高度自适应布局
    VC#窗体的大小设置
  • 原文地址:https://www.cnblogs.com/dhqy/p/8694613.html
Copyright © 2011-2022 走看看