包含列表的对象的 ObservableCollection
我使用 ObservableCollection 进行数据绑定作为 DataGrid 的 ItemsSource。集合包含复杂类型的对象。这种类型的属性之一是字符串列表。
就目前而言,我发现当我从代码更新此 List 属性时,UI 中没有任何变化(主绑定工作正常)。所以,我的问题是:这是预期的行为吗?也许我不应该使用 List 作为类型的一部分,而应该使用 ObservableCollection?
更新
模式设置为单向。
I am using an ObservableCollection for databinding as ItemsSource for DataGrid. Collection contains complex type objects. One of this type properties is a List of strings.
Just for now I see that when I update this List property from code nothing changes in the UI (the primary binding works fine). So, my question is: is it an expected behaviour? Maybe I should not use List as part of the type, but also use an ObservableCollection?
Update
Mode is set to OneWay.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用实现接口 INotifyCollectionChanged 的集合,而不是 List (例如 ObservableCollection)。然后对集合的更改将填充到 ui 中。
Use a collection, instead of List, that implementes the interface INotifyCollectionChanged (like ObservableCollection). Then changes to the collection get populated to the ui.
是的,这是预期的行为。可观察集合仅通知其自身内容的更改 - 即添加、删除、重新排序。
您所看到的是对 observablecollection 中元素的更改 - 如果您想查看对放入的类的更改,您的元素必须实现 INotifyPropertyChanged。
所以目前:如果复杂对象的列表属性发生变化,您将看不到它,但是如果您将其也更改为可观察集合,您可以在子项控件(如组合框)中看到该集合的更改 - 但如果您更改则不会集合对象到另一个集合对象 - 因此,如果您不实现 INotifyPropertyChanged,则应在应用绑定之前设置集合属性。
Yes it is expected behaviour. The observable collection only notifies of changes to its own contents - that is add, delete, reorder.
What you are looking at is a change to an element in the observablecollection - if you want to see your changes to the class you put in, your element has to implement INotifyPropertyChanged.
So currently: If your list property on you complex object changes you won't see it, however if you change that too to be an observablecollection you could see changes to that collection in a sub-itemscontrol like a combobox - but not if you change the collection object to another one - so if you do not implement INotifyPropertyChanged you should set the collectionproperty before the binding is applied.
当您更新列表时,您必须调用 INotifyPropertyChange,否则 UI 将无法更新列表结果。
INotifyPropertyChange 表明项目源中发生了一些更改,因此请更新它。
When you are updateding your list u have to call INotifyPropertyChange other wise UI wont get update the list result..
INotifyPropertyChange is the indication that here some changes occurred in the items source so update it.
这也可能有帮助:
ObservableCollection 还监视更改集合中的元素
This might help as well:
ObservableCollection that also monitors changes on the elements in collection