如何在WPF ItemsControl中重写预定义的依赖项Properties ItemsSource的PropertyChangedCallback [英] How to Override PropertyChangedCallback of a predefined Dependency Property ItemsSource in a WPF ItemsControl
问题描述
如何覆盖 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.
请协助我,要覆盖的方法名称是什么? ...
推荐答案
在中调用 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屋!