如何在WPF ItemsControl中重写预定义的依赖项Properties ItemsSource的PropertyChangedCallback [英] How to Override PropertyChangedCallback of a predefined Dependency Property ItemsSource in a WPF ItemsControl

查看:201
本文介绍了如何在WPF ItemsControl中重写预定义的依赖项Properties ItemsSource的PropertyChangedCallback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何覆盖 PropertyChangedCallback 预定义的依赖项属性 ItemsSource 在WPF ItemsControl 中。

我开发了继承的WPF自定义控件来自 ItemsControl 。在此,我使用了预定义的依赖项属性 ItemsSource 。这样,一旦 Collection 得到更新,我就需要监视和检查数据。

I developed a WPF Custom Control inherited from ItemsControl. In that I used the predefined Dependency Property ItemsSource. In that I need to monitor and check data once the Collection gets updated.

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

I searched a lot in google, but I can't able to find any related solution to fulfill my requirement.

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

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

推荐答案

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

Call OverrideMetadata in a static constructor of your derived ItemsSource 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
    }
}

这篇关于如何在WPF ItemsControl中重写预定义的依赖项Properties ItemsSource的PropertyChangedCallback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆