locked
Is it somehow possible to create a event when a collection is changed? RRS feed

  • Question

  • Hello,

    I am wondering if there is someway to fire off a event when
    System.Collections.ObjectModel.ReadOnlyObserveableCollection<Column> mycolumn     is changed?
     (Either the positioning is changed inside / or it was recreated / or if a new one is added / or when a column is removed)


    Thanks in advance!








    none
    Tuesday, September 30, 2008 9:37 PM

Answers

  • You mean other than hooking up to the CollectionChanged event??? This event is sent for all the changes you mentioned. If you want to know exactly which kind of change it was, check the enum that is passed in Action property of the event args that are passed into your event handler.

    HTH,
    Drew

    • Marked as answer by Drew Marsh Wednesday, October 1, 2008 12:39 PM
    • Marked as answer by ashlar Wednesday, October 1, 2008 1:05 PM
    Wednesday, October 1, 2008 1:12 AM
  •  
    ...  
     
    // Hook up the event handler  
    myCollection.NotifyCollectionChanged += this.MyCollectionChanged;  
     
    ...  
     
    // The event handler  
    private void MyCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)  
    {  
       Debug.WriteLine("My collection was changed! Action = " + args.Action.ToString());  
    • Marked as answer by Drew Marsh Wednesday, October 1, 2008 12:39 PM
    Wednesday, October 1, 2008 12:39 PM
  • Thanks for the help guys....I see what was happening....The CollectionChanged event was protected but there was a way to get around it....this link shows how.

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5327c7d4-ef16-49e6-a8c0-c33ce06f87a2/
    none
    • Marked as answer by ashlar Wednesday, October 1, 2008 1:05 PM
    Wednesday, October 1, 2008 1:05 PM

All replies

  • You mean other than hooking up to the CollectionChanged event??? This event is sent for all the changes you mentioned. If you want to know exactly which kind of change it was, check the enum that is passed in Action property of the event args that are passed into your event handler.

    HTH,
    Drew

    • Marked as answer by Drew Marsh Wednesday, October 1, 2008 12:39 PM
    • Marked as answer by ashlar Wednesday, October 1, 2008 1:05 PM
    Wednesday, October 1, 2008 1:12 AM
  • Hello,

    That sounds like what I need.  Could someone provide a small code sample on how you go about hooking up to the CollectionChanged event?
    none
    Wednesday, October 1, 2008 12:32 PM
  •  
    ...  
     
    // Hook up the event handler  
    myCollection.NotifyCollectionChanged += this.MyCollectionChanged;  
     
    ...  
     
    // The event handler  
    private void MyCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)  
    {  
       Debug.WriteLine("My collection was changed! Action = " + args.Action.ToString());  
    • Marked as answer by Drew Marsh Wednesday, October 1, 2008 12:39 PM
    Wednesday, October 1, 2008 12:39 PM
  • Thanks for the help guys....I see what was happening....The CollectionChanged event was protected but there was a way to get around it....this link shows how.

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5327c7d4-ef16-49e6-a8c0-c33ce06f87a2/
    none
    • Marked as answer by ashlar Wednesday, October 1, 2008 1:05 PM
    Wednesday, October 1, 2008 1:05 PM