Wpf ObservableCollection作为DependencyProperty-如何在CollectionChanged事件中添加/删除项?
我有以下代码:Wpf ObservableCollection作为DependencyProperty-如何在CollectionChanged事件中添加/删除项?,wpf,Wpf,我有以下代码: public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.RegisterAttached("ItemsSource", typeof (ObservableCollection<BaseViewModel>), typeof (MultiSelectComboBoxUserControl), new FrameworkPropertyMetadata(null
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.RegisterAttached("ItemsSource", typeof (ObservableCollection<BaseViewModel>),
typeof (MultiSelectComboBoxUserControl),
new FrameworkPropertyMetadata(null, OnItemsSourceChanged));
public static ObservableCollection<BaseViewModel> GetItemsSource(DependencyObject obj)
{
return (ObservableCollection<BaseViewModel>) obj.GetValue(ItemsSourceProperty);
}
public static void SetItemsSource(DependencyObject obj, ObservableCollection<BaseViewModel> value)
{
obj.SetValue(ItemsSourceProperty, value);
}
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != null)
{
var coll = (INotifyCollectionChanged) e.OldValue;
coll.CollectionChanged -= ItemsSource_CollectionChanged;
}
if (e.NewValue != null)
{
var coll = (ObservableCollection<BaseViewModel>) e.NewValue;
coll.CollectionChanged += ItemsSource_CollectionChanged;
}
}
private static void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
//Here I'd like to update my ObservableCollection - ItemsSource
}
公共静态只读依赖项属性项资源属性=
DependencyProperty.RegisterAttached(“ItemsSource”,类型为(ObservableCollection),
类型(MultiSelectComboxUserControl),
新的FrameworkPropertyMetadata(null,OnItemSourceChanged));
公共静态ObservableCollection GetItemsSource(DependencyObject obj)
{
返回(ObservableCollection)对象GetValue(ItemsSourceProperty);
}
公共静态void SetItemsSource(DependencyObject对象,ObservableCollection值)
{
对象设置值(ItemsSourceProperty,value);
}
私有静态资源已更改(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
如果(e.OldValue!=null)
{
var coll=(INotifyCollectionChanged)e.OldValue;
coll.CollectionChanged-=项目资源集合变更;
}
如果(如NewValue!=null)
{
var coll=(可观测集合)e.NewValue;
coll.CollectionChanged+=项目资源集合变更;
}
}
私有静态无效项Source\u CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
//这里我想更新我的ObservableCollection-ItemsSource
}
我如何实现这一点(更新ItemsSource)?我无法访问它,因为它是一个依赖属性,而事件处理程序是一个静态方法。欢迎提供任何提示。作为
发送方
参数接收的对象是可观察收集
。如果计划更改集合(例如添加或删除项),则可能还需要在进行更改之前分离事件处理程序
private static void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
ObservableCollection collection = sender as ObservableCollection<BaseViewModel>;
if (collection != null)
{
collection.CollectionChanged -= ItemsSource_CollectionChanged;
//Update ObservableCollection
collection.CollectionChanged += ItemsSource_CollectionChanged;
}
}
private static void ItemsSource\u CollectionChanged(对象发送方,notifycollectionchangedventargs e)
{
ObservableCollection collection=发送方作为ObservableCollection;
if(集合!=null)
{
collection.CollectionChanged-=ItemsSource\u CollectionChanged;
//更新可观测集合
collection.CollectionChanged+=项目资源\集合更改;
}
}
我根本不会更改ItemsSource实例。我只需创建我的ObservableCollection实例,并在更新时清除、添加项目。好的,很简单
旧代码:
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgse)
{
if (e.OldValue != null)
{
var coll = (INotifyCollectionChanged) e.OldValue;
coll.CollectionChanged -= ItemsSource_CollectionChanged;
}
if (e.NewValue != null)
{
var coll = (ObservableCollection<BaseViewModel>) e.NewValue;
coll.CollectionChanged += ItemsSource_CollectionChanged;
}
}
私有静态void OnItemSourceChanged(DependencyObject d,DependencyPropertyChangedEventArgse)
{
如果(e.OldValue!=null)
{
var coll=(INotifyCollectionChanged)e.OldValue;
coll.CollectionChanged-=项目资源集合变更;
}
如果(如NewValue!=null)
{
var coll=(可观测集合)e.NewValue;
coll.CollectionChanged+=项目资源集合变更;
}
}
新代码:
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgse)
{
(YouCustomControl) control = (YouCustomControl)d;
if (e.OldValue != null)
{
var coll = (INotifyCollectionChanged) e.OldValue;
coll.CollectionChanged -= control.ItemsSource_CollectionChanged;
}
if (e.NewValue != null)
{
var coll = (ObservableCollection<BaseViewModel>) e.NewValue;
coll.CollectionChanged += control.ItemsSource_CollectionChanged;
}
}
private void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
}
私有静态void OnItemSourceChanged(DependencyObject d,DependencyPropertyChangedEventArgse)
{
(YouCustomControl)control=(YouCustomControl)d;
如果(e.OldValue!=null)
{
var coll=(INotifyCollectionChanged)e.OldValue;
coll.CollectionChanged-=control.ItemsSource\u CollectionChanged;
}
如果(如NewValue!=null)
{
var coll=(可观测集合)e.NewValue;
coll.CollectionChanged+=control.ItemsSource\u CollectionChanged;
}
}
私有无效项资源\集合更改(对象发送方,NotifyCollectionChangedEventArgs e)
{
}