我如何在WPF中刷新ListView

Jit*_*dav 21 wpf listview

您好我正在使用WPF并逐个添加记录到listview.ItemsSource.当包含所有数据时,我的数据将会出现,但我希望在逐个添加数据时显示数据.

我使用了ListView.Item.Refresh()但它没有用.

有什么办法吗?

谢谢...

Eug*_*rda 36

如果您仍需要在任何其他情况下刷新ListView(假设您需要在将所有元素添加到ItemsSource后更新一次),那么您应该使用此方法:

ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
view.Refresh();
Run Code Online (Sandbox Code Playgroud)


dec*_*one 21

例:

// Create a collection of Type System.Collections.ObjectModel.ObservableCollection<T>
// Here T can be anything but for this example, we use System.String
ObservableCollection<String> names = new ObservableCollection<String>();

// Assign this collection to ItemsSource property of ListView
ListView1.ItemsSource = names;

// Start adding items to the collection
// They automatically get added to ListView without a need to write any extra code
names.Add("Name 1");
names.Add("Name 2");
names.Add("Name 3");
names.Add("Name 4");
names.Add("Name 5");

// No need to call ListView1.Items.Refresh() when you use ObservableCollection<T>.
Run Code Online (Sandbox Code Playgroud)


Guy*_*Guy 6

您需要绑定到它实现了一个集合INotifyCollectionChanged,例如ObservableCollection<T>.每当添加或删除项目时,此接口都会通知绑定控件(因此您根本不需要进行任何调用).

链接到INotifyCollectionChanged接口

System.Windows.Controls.ListView没有名为Item的成员,请确保您没有尝试从成员上调用方法System.Windows.Forms.ListView.Referance:MSDN