ObservableCollection as DependencyProperty - 如何在CollectionChanged事件中添加/删除项目?

时间:2014-02-23 23:25:08

标签: wpf

我有以下代码:

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
 }

我如何实现这一点(更新ItemsSource)?我无法访问它,因为它是一个依赖属性,事件处理程序是一个静态方法。任何提示都非常欢迎。

3 个答案:

答案 0 :(得分:0)

您作为sender参数收到的对象是ObservableCollection。如果您打算更改集合(例如添加或删除项目),您可能还需要在进行更改之前分离事件处理程序。

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;
    }
}

答案 1 :(得分:0)

我根本不会更改我的ItemsSource实例。我只需创建我的ObservableCollection实例并清除,在更新时添加项目。

答案 2 :(得分:0)

好的,很简单!

旧代码:

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;
 }
}

新守则:

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)
{
}