我如何在WPF中刷新ListView

时间:2022-08-25 09:06:13

Hi I am using WPF and adding records one by one to the listview.ItemsSource. My data will appear when all the data is included, but I want to show the data as it is added one by one.

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

I used ListView.Item.Refresh() but it didn't work.

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

Is there any way?

有什么办法吗?

Thanks...

谢谢...

5 个解决方案

#1


31  

If you still need to refresh your ListView in any other case (lets assume that you need to update it ONE time after ALL the elements were added to the ItemsSource) so you should use this approach:

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

ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
view.Refresh();

#2


16  

Example:

// 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>.

#3


5  

You need to bind to a collection which implements INotifyCollectionChanged, for example ObservableCollection<T>. This interface notifies the bound control whenever an item is added or removed (so you dont have to make any call at all).

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

Link to INotifyCollectionChanged Interface

链接到INotifyCollectionChanged接口

Also System.Windows.Controls.ListView doesnt have a member named Item, make sure you are not trying to call a method on a member from System.Windows.Forms.ListView. Referance: MSDN

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

#4


1  

@decyclone:

@decyclone:

I'm working in WPF the idea is to have a tree view that we can dynamically add and remove elements - files. The ObservableCollection was the method for adding (using drag and drop and an open dialog box for files)

我在WPF工作的想法是有一个树视图,我们可以动态添加和删除元素 - 文件。 ObservableCollection是添加的方法(使用拖放和文件的打开对话框)

ObservableCollection worked fine for adding but items removal was not being displayed correctly. The refresh method did not "refresh". The solution was to reset (again) the listview.ItemSource to the new values (the list without the elements that were removed).

ObservableCollection适用于添加,但项目删除未正确显示。刷新方法没有“刷新”。解决方案是将listview.ItemSource重置(再次)为新值(没有删除元素的列表)。

Thanks for your answer since it helped me.

谢谢你的回答,因为它帮助了我。

#5


-1  

ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
view.Refresh();

Its really work to refresh the ListView.

它确实可以刷新ListView。

#1


31  

If you still need to refresh your ListView in any other case (lets assume that you need to update it ONE time after ALL the elements were added to the ItemsSource) so you should use this approach:

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

ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
view.Refresh();

#2


16  

Example:

// 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>.

#3


5  

You need to bind to a collection which implements INotifyCollectionChanged, for example ObservableCollection<T>. This interface notifies the bound control whenever an item is added or removed (so you dont have to make any call at all).

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

Link to INotifyCollectionChanged Interface

链接到INotifyCollectionChanged接口

Also System.Windows.Controls.ListView doesnt have a member named Item, make sure you are not trying to call a method on a member from System.Windows.Forms.ListView. Referance: MSDN

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

#4


1  

@decyclone:

@decyclone:

I'm working in WPF the idea is to have a tree view that we can dynamically add and remove elements - files. The ObservableCollection was the method for adding (using drag and drop and an open dialog box for files)

我在WPF工作的想法是有一个树视图,我们可以动态添加和删除元素 - 文件。 ObservableCollection是添加的方法(使用拖放和文件的打开对话框)

ObservableCollection worked fine for adding but items removal was not being displayed correctly. The refresh method did not "refresh". The solution was to reset (again) the listview.ItemSource to the new values (the list without the elements that were removed).

ObservableCollection适用于添加,但项目删除未正确显示。刷新方法没有“刷新”。解决方案是将listview.ItemSource重置(再次)为新值(没有删除元素的列表)。

Thanks for your answer since it helped me.

谢谢你的回答,因为它帮助了我。

#5


-1  

ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
view.Refresh();

Its really work to refresh the ListView.

它确实可以刷新ListView。