WPF:当项目在ListView中添加触发事件

编程入门 行业动态 更新时间:2024-10-27 00:35:25
本文介绍了WPF:当项目在ListView中添加触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我工作的WPF和我使用一个ListView,我需要当一个项目被添加到它来触发一个事件。我曾尝试这样的:

I am working on WPF and I am using a ListView, and I need to fire an event when an item is added to it. I have tried this:

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,但最好的答案适用于只有一个列表框。我试图改变code到ListView的,但我不是能够做到这一点。

But It seems to be working only when the entire collection is changed, I have read this post: event-fired-when-item-is-added-to-listview, but the best answer applies for a listBox only. I tried to change the code to ListView but I wasnt able to do that.

我希望你能帮助我。谢谢你在前进。

I hope You can help me. Thank you in advance.

推荐答案

的注意一个WPF列表视图这只是工程!的

经过一番研究,我已经找到了答案,我的问题,这真的很容易:

After some research I have found the answer to my question and It's really easy:

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 枚举允许程序通知你有关的任何变化,如:添加,移动,替换,删除和重置

Actually, the NotifyCollectionChangedAction enum allows your program to inform you about any change such as: Add, Move, Replace, Remove and Reset.

更多推荐

WPF:当项目在ListView中添加触发事件

本文发布于:2023-11-10 22:57:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1576693.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   项目   WPF   ListView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!