WPF:如何挂钩 ListView 的 ItemsSource CollectionChanged 通知?
我有一个 ListView
数据绑定到 ObservableCollection
...
<ListView x:Name="List1" ItemsSource="{Binding MyList}" />
我似乎找不到集合更改时触发的任何事件,所以我想不知何故,我需要以某种方式连接到集合更改通知?我不太确定该怎么做。
基本上,当集合发生变化时,我想做的工作超出了 ListView 在更新其列表方面已经完成的工作。
I have a ListView
that is databound to an ObservableCollection
...
<ListView x:Name="List1" ItemsSource="{Binding MyList}" />
I can't seem to find any event that are triggered when the collection changes, so I'm thinking that somehow I need to hook into the collectionchanged notification somehow? I'm not really sure how to do that.
Basically, when the collection changes I want to do additional work beyond what the ListView already does in updating it's list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,
ItemsSource
的类型为 <代码>IEnumerable。您需要首先转换为可以访问CollectionChanged
事件,然后为该事件添加处理程序。Note: I cast it to
INotifyCollectionChanged
in my example, but you can really cast it to any object that implements that. Though, as a best practice, you should cast to the most generic type that gives you access to the methods/properties/events you need. So, while you can cast it to anObservableCollection
, you don't need to.INotifyCollectionChanged
contains the event you need and if you ever decide to use some other type of collection that implements it, this will continue to work, whereas casting to anObservableCollection
means that if you one day decide that you're list is now of typeMyOwnTypeOfObservableCollectionNotDerivedFromObservableCollection
than this will break. ;)PS 这应该放在 xaml 代码隐藏中。
By default the
ItemsSource
is of typeIEnumerable
. You need to first cast to a type that has access to theCollectionChanged
event, then add a handler for that event.Note: I cast it to
INotifyCollectionChanged
in my example, but you can really cast it to any object that implements that. Though, as a best practice, you should cast to the most generic type that gives you access to the methods/properties/events you need. So, while you can cast it to anObservableCollection
, you don't need to.INotifyCollectionChanged
contains the event you need and if you ever decide to use some other type of collection that implements it, this will continue to work, whereas casting to anObservableCollection
means that if you one day decide that you're list is now of typeMyOwnTypeOfObservableCollectionNotDerivedFromObservableCollection
than this will break. ;)P.S. This should go in the xaml code-behind.
您必须将处理程序附加到您的列表中。或者,使用 < code>CollectionView 并在那里挂钩更改的事件。
在你的代码隐藏中,这样做:
you are going to have to attach the handler to your list. Or, use a
CollectionView
and hook the changed event there.in your codebehind, do like this:
ObservableCollection{T} 公开 INotifyCollectionChanged.CollectionChanged< /a> 事件。当绑定到 ItemsSource 时,数据绑定引擎会处理从源到项目控件的更改,但如果您需要执行其他处理,您可以将处理程序附加到 CollectionChanged 事件并使用 NotifyCollectionChangedEventArgs 它提供了。
假设您的视图模型上有一个名为 MyList 的公共属性:
An ObservableCollection{T} exposes the INotifyCollectionChanged.CollectionChanged event. When binding to an ItemsSource the data binding engine handles the propogation of changes from the source to the items control, but if you need to perform additional processing you can attach a handler to the CollectionChanged event and use the NotifyCollectionChangedEventArgs it provides.
Assuming you have a public property on your view model named MyList: