ListView与observablecollection的双向绑定不会自动更新
public ObservableCollection<App> Apps { get; set; }
public MainWindow()
{
Apps = LoadApps();
listview.ItemsSource = Apps;
}
public void AnotherMethod()
{
Apps = LoadApps();
}
当分配给 ItemsSource 时它可以工作,但我需要每次应用程序内容更改时更新 ListView。
public ObservableCollection<App> Apps { get; set; }
public MainWindow()
{
Apps = LoadApps();
listview.ItemsSource = Apps;
}
public void AnotherMethod()
{
Apps = LoadApps();
}
It works when assigned to ItemsSource but I need the ListView to be updated every time the Apps content is changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ObservableCollection 仅在更新集合上的项目时才更新 ListView 项目。
您可以执行类似操作
或将 Apps 定义为依赖项属性并将其绑定到列表视图 ItemSource:
ObservableCollection only update ListView items when items on collection was updated.
you can do something like
or define Apps as a Dependency Property and bind it to list view ItemSource:
“应用程序内容”是什么意思?如果将整个引用替换为新集合,则需要实施 < a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx" rel="nofollow">
MainWindow
中的INotifyPropertyChanged
。如果项目内部发生变化并且您希望在ListView
中进行更新,则需要在App
中实现所述接口。(请注意第一种可能性:通常您不会允许通过创建
私有只读
字段来替换引用,显然公开它的公共属性只会有一个 getter。)What do you mean by "Apps content"? If you replace the whole reference with a new collection you need to implement
INotifyPropertyChanged
inMainWindow
. If the items change internally and you want updates in theListView
you need to implement said interface inApp
.(Note about the first possibility: Often you would not allow the replacement of the reference by creating a
private readonly
field, obviously the public property exposing it would only have a getter.)