开发者

CollectionChanged event of observablecollection in c# [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. 开发者_Python百科 Closed 10 years ago.

How can write this code better:

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. When e.Action == NotifyCollectionChangedAction.Remove you need to iterate over e.OldItems instead of e.NewItems.
  2. When e.Action == NotifyCollectionChangedAction.Replace you need to iterate over e.OldItems to remove the event handler from the old items and you need to iterate over e.NewItems to add the event handler to the new items.
  3. Refactor the code like this:

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

This works, because all of your Promotion classes implement INotifyPropertyChanged.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜