WPF ListView即时更新

本文介绍了如何在WPF应用程序中使ListView控件能够实时更新。主要通过使用BindingList<T>作为ListView的ItemSource,并确保对应的Item类实现了INotifyPropertyChanged接口来实现。当属性更改时,通过触发PropertyChanged事件通知UI进行更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文: WPF 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
  }

posted on 2018-08-07 23:50 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/9440312.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值