C# 更改 DataGrid.ItemsSource 时如何引发事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10708806/
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 14:55:48  来源:igfitidea点击:

How to raise an event when DataGrid.ItemsSource is changed

c#wpfeventsdatagrid

提问by Dante

I am new in WPF, and I am working with DataGrids and I need to know when the property ItemsSource is changed.

我是 WPF 的新手,我正在使用 DataGrids,我需要知道何时更改了属性 ItemsSource。

For example, I would need that when this instruction is executed an event has to raise:

例如,我需要在执行此指令时必须引发一个事件:

dataGrid.ItemsSource = table.DefaultView;

Or when a row is added.

或者当添加一行时。

I have tried to use this code:

我曾尝试使用此代码:

CollectionView myCollectionView = (CollectionView)CollectionViewSource.GetDefaultView(myGrid.Items);
((INotifyCollectionChanged)myCollectionView).CollectionChanged += new NotifyCollectionChangedEventHandler(DataGrid_CollectionChanged); 

But this code works only when the user adds a new row to the collection. Therefore I need a event that be raised when the entire ItemsSource property has any change, either because the entire collection is replaced or because a single row is added.

但此代码仅在用户向集合中添加新行时才有效。因此,我需要一个在整个 ItemsSource 属性发生任何更改时引发的事件,要么是因为整个集合被替换,要么是因为添加了一行。

I hope you can help me. Thank you in advance

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

采纳答案by vcsjones

ItemsSourceis a dependency property, so it's easy enough to be notified when the property is changed to something else. You would want to use this in addition to code that you have, not instead of:

ItemsSource是一个依赖属性,因此很容易在属性更改为其他内容时收到通知。除了您拥有的代码之外,您还想使用它,而不是:

In Window.Loaded(or similar) you can subscribe like so:

Window.Loaded(或类似的)中,您可以像这样订阅:

var dpd = DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty, typeof(DataGrid));
if (dpd != null)
{
    dpd.AddValueChanged(myGrid, ThisIsCalledWhenPropertyIsChanged);
}

And have a change handler:

并有一个更改处理程序:

private void ThisIsCalledWhenPropertyIsChanged(object sender, EventArgs e)
{
}

Whenever the ItemsSourceProperty is set, the ThisIsCalledWhenPropertyIsChangedmethod is called.

只要ItemsSource设置了属性,ThisIsCalledWhenPropertyIsChanged就会调用该方法。

You can use this for anydependency property you want to be notified about changes.

您可以将它用于您希望收到有关更改通知的任何依赖项属性。

回答by Phil

Is this any help?

这有什么帮助吗?

public class MyDataGrid : DataGrid
{
    protected override void OnItemsSourceChanged(
                                    IEnumerable oldValue, IEnumerable newValue)
    {
        base.OnItemsSourceChanged(oldValue, newValue);

        // do something here?
    }

    protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
    {
        base.OnItemsChanged(e);

        switch (e.Action)
        {
            case NotifyCollectionChangedAction.Add:
                break;
            case NotifyCollectionChangedAction.Remove:
                break;
            case NotifyCollectionChangedAction.Replace:
                break;
            case NotifyCollectionChangedAction.Move:
                break;
            case NotifyCollectionChangedAction.Reset:
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }
}

回答by yu yang Jian

If you wnat to detect new row added can try DataGrid's InitializingNewItemor AddingNewItemEvent.

如果您想检测添加的新行,可以尝试使用 DataGridInitializingNewItemAddingNewItemEvent。

InitializingNewItemusage :

InitializingNewItem用法 :

Datagrid auto add item with data of parent

Datagrid 自动添加带有父级数据的项目