如何重写 WPF ItemsControl 中预定义依赖项 属性 ItemsSource 的 属性ChangedCallback

How to Override PropertyChangedCallback of a predefined Dependency Property ItemsSource in a WPF ItemsControl

如何覆盖 PropertyChangedCallback 预定义的依赖关系 属性 ItemsSource 在 WPF ItemsControl.

我开发了一个继承自ItemsControl的WPF自定义控件。因为我使用了预定义的依赖关系 属性 ItemsSource。因为我需要在 Collection 更新后监控和检查数据。

我在google中搜索了很多,但找不到任何相关的解决方案来满足我的要求。

https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemssource(v=vs.110).aspx

请帮助我,覆盖的方法名称是什么?...

在派生的 ItemsSource 的静态构造函数中调用 OverrideMetadata class:

public class MyItemsControl : ItemsControl
{
    static MyItemsControl()
    {
        ItemsSourceProperty.OverrideMetadata(
            typeof(MyItemsControl),
            new FrameworkPropertyMetadata(OnItemsSourcePropertyChanged));
    }

    private static void OnItemsSourcePropertyChanged(
        DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        ((MyItemsControl)obj).OnItemsSourcePropertyChanged(e);
    }

    private void OnItemsSourcePropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        var oldCollectionChanged = e.OldValue as INotifyCollectionChanged;
        var newCollectionChanged = e.NewValue as INotifyCollectionChanged;

        if (oldCollectionChanged != null)
        {
            oldCollectionChanged.CollectionChanged -= OnItemsSourceCollectionChanged;
        }

        if (newCollectionChanged != null)
        {
            newCollectionChanged.CollectionChanged += OnItemsSourceCollectionChanged;
            // in addition to adding a CollectionChanged handler
            // any already existing collection elements should be processed here
        }
    }

    private void OnItemsSourceCollectionChanged(
        object sender, NotifyCollectionChangedEventArgs e)
    {
        // handle collection changes here
    }
}