?
Solved

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

Posted on 2010-11-18
1
Medium Priority
?
405 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 2000 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

Industry Leaders: 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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 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