c#中的observablecollection的CollectionChanged事件

时间:2011-09-06 07:51:38

标签: c# events

如何更好地编写此代码:

void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    if (sender is ObservableCollection<PromotionPurchaseAmount>)
    {
        if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            foreach (PromotionPurchaseAmount item in e.NewItems)
            {
                //Removed items
                item.PropertyChanged -= EntityViewModelPropertyChanged;
            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (PromotionPurchaseAmount item in e.NewItems)
            {
                //Added items
                item.PropertyChanged += EntityViewModelPropertyChanged;
            }
        }
    }
    else if (sender is ObservableCollection<PromotionItemPricing>)
    {
        if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            foreach (PromotionItemPricing item in e.NewItems)
            {
                //Removed items
                item.PropertyChanged -= EntityViewModelPropertyChanged;
            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (PromotionItemPricing item in e.NewItems)
            {
                //Added items
                item.PropertyChanged += EntityViewModelPropertyChanged;
            }
        }
    }
    else if (sender is ObservableCollection<PromotionItem>)
    {
        if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            foreach (PromotionItem item in e.NewItems)
            {
                //Removed items
                item.PropertyChanged -= EntityViewModelPropertyChanged;
            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (PromotionItem item in e.NewItems)
            {
                //Added items
                item.PropertyChanged += EntityViewModelPropertyChanged;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:6)

  1. e.Action == NotifyCollectionChangedAction.Remove需要迭代e.OldItems而不是e.NewItems
  2. e.Action == NotifyCollectionChangedAction.Replace需要迭代e.OldItems以从旧项中删除事件处理程序时,您需要迭代e.NewItems以将事件处理程序添加到新项目中。< / LI>
  3. 重构代码如下:

    if (sender is ObservableCollection<PromotionPurchaseAmount> || 
        sender is ObservableCollection<PromotionItemPricing> || 
        sender is ObservableCollection<PromotionItem>)
    {
        if (e.Action == NotifyCollectionChangedAction.Remove ||
            e.Action == NotifyCollectionChangedAction.Replace)
        {
    
            foreach (INotifyPropertyChanged item in e.OldItems)
            {
                //Removed items
                item.PropertyChanged -= EntityViewModelPropertyChanged;
            }
        }
        if (e.Action == NotifyCollectionChangedAction.Add ||
            e.Action == NotifyCollectionChangedAction.Replace)
        {
            foreach (INotifyPropertyChanged item in e.NewItems)
            {
                //Added items
                item.PropertyChanged += EntityViewModelPropertyChanged;
            }
        }
    }
    
  4. 这很有效,因为所有的Promotion类都实现了INotifyPropertyChanged