Question

I need to refresh my ListView every time the another control's IsChanged event is fired. I googled on how to do that, and I saw a stackoverflow link that led me here

One of the answers worked:

listView.ItemsSource = listView.ItemsSource    

Is that really the only way to refresh my ListView? It feels kinda off.

Was it helpful?

Solution

Just invalidate it.

listView.InvalidateProperty(ListView.ItemsSourceProperty)

That ought to do it.

As an aside, I would really suggest looking at MVVM. It tends to be much more powerful. In this case, for an MVVM application, I would just do this:

Xaml:

<ListView ItemsSource="{Binding MyItems}" />

And here would be my ViewModel I'm binding to:

public ObservableCollection<MyItem> MyItems
{
     get; set;
}

public void IsChangedHandler(...)
{
     ...
     this.OnPropertyChanged("MyItems");
}

OTHER TIPS

What is your need to refresh the listview each time. It will definitly slow down the performance of your appliction.

It is better to use an ObervableCollection as your listview's ItemSource.

You can find a Thread safe observable collection here.

Also see this question in MSDN Forum - ListView.ItemsSource: howto update the UI whenever the source is updated?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top