c# – WPF:在ListView中添加项目时提高事件
发布时间:2020-12-15 03:55:47 所属栏目:百科 来源:网络整理
导读:我正在使用 WPF,我正在使用一个ListView,当我添加一个项目时,我需要触发一个事件.我试过这个: var dependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty,typeof(ListView)); if (dependencyPropert
我正在使用
WPF,我正在使用一个ListView,当我添加一个项目时,我需要触发一个事件.我试过这个:
var dependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty,typeof(ListView)); if (dependencyPropertyDescriptor != null) { dependencyPropertyDescriptor.AddValueChanged(this,ItemsSourcePropertyChangedCallback); } ….. private void ItemsSourcePropertyChangedCallback(object sender,EventArgs e) { RaiseItemsSourcePropertyChangedEvent(); } 但是,似乎只有当整个集合被改变时,我已经阅读了这篇文章:event-fired-when-item-is-added-to-listview,但最好的答案只适用于列表框.我试图将代码更改为ListView,但我无法做到这一点. 我希望你能帮助我.先谢谢你. 解决方法
注意这只适用于WPF Listview!
经过一番研究,我找到了我的问题的答案,这很简单: public MyControl() { InitializeComponent(); ((INotifyCollectionChanged)listView.Items).CollectionChanged += ListView_CollectionChanged; } private void ListView_CollectionChanged(object sender,NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Add) { // scroll the new item into view listView.ScrollIntoView(e.NewItems[0]); } } 实际上,NotifyCollectionChangedAction枚举允许您的程序通知您有任何更改,如:添加,移动,替换,删除和重置. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |