A more elegant ListView requery

末鹿安然 提交于 2019-12-11 02:04:12

问题


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.


回答1:


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");
}



回答2:


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?



来源:https://stackoverflow.com/questions/1406542/a-more-elegant-listview-requery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!