Solved

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

Posted on 2010-11-18
1
393 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is Thread.Sleep? 5 46
Error in page 3 46
Export import database 4 43
Automated testing suggestions? 2 28
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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now