Solved

How to write a method to handle the event raised rom this class in C#?

Posted on 2010-11-18
1
403 Views
Last Modified: 2013-12-17
Hi,
I'm having trouble writing event handlers for the ItemPropertyChanged and CollectionChanged events for this class from the answer to this question on another site, http://stackoverflow.com/questions/3849361/itempropertychanged-not-working-on-observablecollection-why

How would methods that can be attached as CollectionChanged and ItemPropertyChanged handlers be written?

var orders = new ItemObservableCollection<OrderViewModel>(); 
orders.CollectionChanged   += OnOrdersChanged; 
orders.ItemPropertyChanged += OnOrderChanged; 

Open in new window


For this class:
public sealed class ItemObservableCollection<T> : ObservableCollection<T> 
    where T : INotifyPropertyChanged 
{ 
    public event EventHandler<ItemPropertyChangedEventArgs<T>> ItemPropertyChanged; 
 
    protected override void InsertItem(int index, T item) 
    { 
        base.InsertItem(index, item); 
        item.PropertyChanged += item_PropertyChanged; 
    } 
 
    protected override void RemoveItem(int index) 
    { 
        var item= this[index]; 
        base.RemoveItem(index); 
        item.PropertyChanged -= item_PropertyChanged; 
    } 
 
    protected override void ClearItems() 
    { 
        foreach (var item in this) 
        { 
            item.PropertyChanged -= item_PropertyChanged; 
        } 
 
        base.ClearItems(); 
    } 
 
    protected override void SetItem(int index, T item) 
    { 
        var oldItem = this[index]; 
        oldItem.PropertyChanged -= item_PropertyChanged; 
        base.SetItem(index, item); 
        item.PropertyChanged += item_PropertyChanged; 
    } 
 
    private void item_PropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
        OnItemPropertyChanged((T)sender, e.PropertyName); 
    } 
 
    private void OnItemPropertyChanged(T item, string propertyName) 
    { 
        var handler = this.ItemPropertyChanged; 
 
        if (handler != null) 
        { 
             handler(this, new ItemPropertyChangedEventArgs<T>(item, propertyName)); 
        } 
    } 
} 
 
public sealed class ItemPropertyChangedEventArgs<T> : EventArgs 
{ 
    private readonly T _item; 
    private readonly string _propertyName; 
 
    public ItemPropertyChangedEventArgs(T item, string propertyName) 
    { 
        _item = item; 
        _propertyName = propertyName; 
    } 
 
    public T Item 
    { 
        get { return _item; } 
    } 
 
    public string PropertyName 
    { 
        get { return _propertyName; } 
    } 
} 

Open in new window

0
Comment
Question by:endrec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 18

Accepted Solution

by:
Richard Lee earned 500 total points
ID: 34165086
Your method signature should look like this:

var orders = new ItemObservableCollection<OrderViewModel>();
orders.CollectionChanged   += OnOrdersChanged;
orders.ItemPropertyChanged += OnOrderChanged;

DaTribe
private void OnOrderChanged(object sender, ItemPropertyChangedEventArgs<OrderViewModel> e)
{
}

private void OnOrdersChanged(object sender, NotifyCollectionChangedEventArgs e)
{
}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question