Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Trouble with References... 5 25
Data Saving 5 40
Set form below another form 3 26
Convert to Hour/minute textbox and compare 3 36
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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

20 Experts available now in Live!

Get 1:1 Help Now