How to raise an event when DataGrid.ItemsSource is changed

前端 未结 3 1319
陌清茗
陌清茗 2020-12-05 02:39

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

For example, I would need that when this instruction is

3条回答
  •  悲哀的现实
    2020-12-05 03:01

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

提交回复
热议问题