How I Can Refresh ListView in WPF
Asked Answered
F

6

36

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.

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

Is there any way?

Flight answered 20/12, 2010 at 9:44 Comment(1)
I am not quite sure i understand your question. Do you want your items to appear one after another in the listview? Adding items is quite fast, so you would most probably not even notice.Francois
A
52

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:

ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
view.Refresh();
Alamode answered 20/12, 2010 at 11:14 Comment(0)
F
31

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>.
Frazzled answered 20/12, 2010 at 11:19 Comment(0)
A
7

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

Link to INotifyCollectionChanged Interface

Also System.Windows.Controls.ListView doesn'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

Aile answered 20/12, 2010 at 10:55 Comment(0)
R
3

@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)

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

Rummel answered 14/6, 2011 at 16:48 Comment(0)
C
1
    ObservableCollection<int> items = new ObservableCollection<int>();
    lvUsers.ItemsSource = items;

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

No need refresh

Conveyancing answered 17/3, 2020 at 8:29 Comment(0)
M
0

I doing exactly this:

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

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

But the data does not appear in the list until I resize the window. :(

Manciple answered 19/4, 2022 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.