C# 如何强制 WPF ListView 重新查询其 ItemSource?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1111338/
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-08-06 08:30:10  来源:igfitidea点击:

How do you force a WPF ListView to Requery its ItemSource?

c#wpfxmllistviewrefresh

提问by KevinDeus

I'm rewriting its XML itemSource on the fly and want it to use the new data right away...

我正在即时重写其 XML itemSource 并希望它立即使用新数据...

采纳答案by Oscar Foley

You should use an ObervableCollection. When this collection is updated, the ListView is updated.

您应该使用 ObervableCollection。当此集合更新时,ListView 也会更新。

But if for any reason you don′t want to use one, use:

但是,如果出于任何原因您不想使用,请使用:

listView.InvalidateProperty(ListView.ItemsSourceProperty);

or

或者

listView.ItemsSource = listView.ItemsSource;

Check A more elegant ListView requeryfor more info.

查看更优雅的 ListView 重新查询以获取更多信息。

回答by Totty

ListView.DataBind(); MSDN

ListView.DataBind(); MSDN

回答by Botz3000

hmm...

唔...

listView.ItemsSource = listView.ItemsSource?

or you could trigger the PropertyChanged event on the property of your viewmodel in case you are using MVVM.

或者,如果您使用的是 MVVM,您可以在视图模型的属性上触发 PropertyChanged 事件。