当更改List< T>时更新自动ListBox项. [英] Update automatic ListBox items when alter List<T>

查看:55
本文介绍了当更改List< T>时更新自动ListBox项.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框,我用ItemsSourceList<Control>填充它.

I have a ListBox, I populate it with ItemsSource with List<Control>.

但是,当我为此列表删除或添加新控件时,每次都需要重置ListBox ItemsSource

But when I delete or add new control for this List, I need every time reset my ListBox ItemsSource

有什么方法可以使ListBox同步列表内容?

Have any method for ListBox sync List content?

推荐答案

使用ObservableCollection<T>代替使用List<T>.该列表支持WPF的更改通知:

Instead of using a List<T>, use an ObservableCollection<T>. It is a list that supports change notifications for WPF:

// if this isn't readonly, you need to implement INotifyPropertyChanged, and raise
// PropertyChanged when you set the property to a new instance
private readonly ObservableCollection<Control> items = 
    new ObservableCollection<Control>();

public IList<Control> Items { get { return items; } }

这篇关于当更改List&lt; T&gt;时更新自动ListBox项.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆