如何在 WPF 中刷新 ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4488463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:25:11  来源:igfitidea点击:

How I Can Refresh ListView in WPF

wpflistview

提问by Jitendra Jadav

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?

有什么办法吗?

回答by Eugene Cheverda

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();

回答by decyclone

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

回答by Guy

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 don't have to make any call at all).

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

Link to INotifyCollectionChanged Interface

链接到INotifyCollectionChanged 接口

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

System.Windows.Controls.ListView没有名为 Item 的成员,请确保您没有尝试从System.Windows.Forms.ListView. 参考:MSDN

回答by Pimenta

@decyclone:

@去旋风:

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

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

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

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

回答by psw

    ObservableCollection<int> items = new ObservableCollection<int>();
    lvUsers.ItemsSource = items;

    for (int i = 0; i < 100; i++)
    {
        items.Add(i);
    }            

No need refresh

无需刷新