Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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