C# WPF:在 ListView 中添加项目时引发事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10884031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:39:06  来源:igfitidea点击:

WPF: Raise an Event when Item is added in ListView

c#wpfeventslistview

提问by Dante

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:

我正在使用 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();
    }

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.

但它似乎只有在整个集合更改时才有效,我已经阅读了这篇文章:event-fired-when-item-is- added-to-listview,但最佳答案仅适用于 listBox 。我试图将代码更改为 ListView,但我无法做到。

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

我希望你能帮助我。先感谢您。

采纳答案by Dante

Note this only works for a WPF Listview!

请注意,这仅适用于 WPF Listview!

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]);
    }
}

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

实际上,NotifyCollectionChangedAction枚举允许您的程序通知您任何更改,例如:添加、移动、替换、删除和重置。

回答by Arvo Bowen

Note: This solution was meant for a WinForms ListView.

注意:此解决方案适用于 WinForms ListView。

In my case I ended up coming to a fork in the road with 2 choices...

就我而言,我最终遇到了两个选择的岔路口……

(1)Create a custom ListView control that inherits a ListView's class. Then add a new event to be raised when any item is added, deleted, or ListView is cleared. This path seemed really messy and long. Not to mention the other big issue that I would need to replace all my original ListViews with the newly created Custom ListView control. So I passed on this!

(1)创建一个自定义ListView控件,继承一个ListView的类。然后添加一个新事件以在添加、删除任何项目或清除 ListView 时引发。这条路看起来真的很乱很长。更不用说我需要用新创建的自定义 ListView 控件替换所有原始 ListView 的另一个大问题。所以我通过了这个!



(2)With every add, delete, or clear call to the listview I also called another function simulating the CollectionChanged event.

(2)每次添加、删除或清除对列表视图的调用时,我还调用了另一个模拟 CollectionChanged 事件的函数。

Create the new event like function...

创建新事件,如函数...

private void myListViewControl_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    //The projects ListView has been changed
    switch (e.Action)
    {
        case NotifyCollectionChangedAction.Add:
            MessageBox.Show("An Item Has Been Added To The ListView!");
            break;
        case NotifyCollectionChangedAction.Reset:
            MessageBox.Show("The ListView Has Been Cleared!");
            break;
    }
}

Add an item to the ListView elsewhere...

将项目添加到其他地方的 ListView...

ListViewItem lvi = new ListViewItem("ListViewItem 1");
lvi.SubItems.Add("My Subitem 1");
myListViewControl.Items.Add(lvi);
myListViewControl_CollectionChanged(myListViewControl, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, lvi, lvi.Index));

Clear the ListView elsewhere...

清除其他地方的 ListView...

myListViewControl.Items.Clear();
myListViewControl_CollectionChanged(myListViewControl, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));