C# 如何在WPF ItemsControl中重写预定义依赖项属性ItemsSource的PropertyChangedCallback
如何在WPFC# 如何在WPF ItemsControl中重写预定义依赖项属性ItemsSource的PropertyChangedCallback,c#,wpf,dependency-properties,itemscontrol,propertychangelistener,C#,Wpf,Dependency Properties,Itemscontrol,Propertychangelistener,如何在WPFItemsControl中覆盖预定义的依赖项属性ItemsSource的属性ChangedCallback 我开发了一个WPF自定义控件,该控件继承自ItemsControl。因为我使用了预定义的依赖属性ItemsSource。在这方面,我需要在集合更新后监视和检查数据 我在谷歌搜索了很多,但我找不到任何相关的解决方案来满足我的要求 请帮助我,要重写的方法名是什么?…在派生ItemsSource类的静态构造函数中调用重写元数据: public class MyItemsContro
ItemsControl
中覆盖预定义的依赖项属性ItemsSource
的属性ChangedCallback
我开发了一个WPF自定义控件,该控件继承自ItemsControl
。因为我使用了预定义的依赖属性ItemsSource
。在这方面,我需要在集合更新后监视和检查数据
我在谷歌搜索了很多,但我找不到任何相关的解决方案来满足我的要求
请帮助我,要重写的方法名是什么?…在派生ItemsSource类的静态构造函数中调用重写元数据
:
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
}
}
. 这是在ItemsSource
依赖项控件的属性更改回调中调用的。这可能不一定是您要找的…@poke您可以指导我,如何监控集合
更改…您可能会更幸运地观察到Items属性的更改,它实现了CollectionView,因此发生了CollectionChanged事件。但我不知道它会有多好。它表示设置ItemsSource时Items集合为只读。