我正在使用WPF,我使用的是ListView,并且需要在将项目添加到项目时触发事件。我曾经尝试这样做: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,但我无法做到这一点。
我希望你能帮助我。先谢谢你。
在回复您发布的问题的评论中,您会发现“修改了上面的代码,使其更加清晰。此外,这应该与任何ItemsControl(ListBox或ListView)。“ - 你有什么问题特别是? – Slugart
@Slugart谢谢你的回复,我遇到的问题是,方法'BeginInvoke'不接受声明作为例如,它说'DispatcherPriority'不存在 – Dante