Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 69201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:32:11+00:00 2026-05-10T19:32:11+00:00

I have something here that is really catching me off guard. I have an

  • 0

I have something here that is really catching me off guard.

I have an ObservableCollection of T that is filled with items. I also have an event handler attached to the CollectionChanged event.

When you Clear the collection it causes an CollectionChanged event with e.Action set to NotifyCollectionChangedAction.Reset. Ok, that’s normal. But what is weird is that neither e.OldItems or e.NewItems has anything in it. I would expect e.OldItems to be filled with all items that were removed from the collection.

Has anyone else seen this? And if so, how have they gotten around it?

Some background: I am using the CollectionChanged event to attach and detach from another event and thus if I don’t get any items in e.OldItems … I won’t be able to detach from that event.

CLARIFICATION: I do know that the documentation doesn’t outright state that it has to behave this way. But for every other action, it is notifying me of what it has done. So, my assumption is that it would tell me … in the case of Clear/Reset as well.

Below is the sample code if you wish to reproduce it yourself. First off the xaml:

<Window     x:Class='ObservableCollection.Window1'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     Title='Window1'     Height='300'     Width='300' >     <StackPanel>         <Button x:Name='addButton' Content='Add' Width='100' Height='25' Margin='10' Click='addButton_Click'/>         <Button x:Name='moveButton' Content='Move' Width='100' Height='25' Margin='10' Click='moveButton_Click'/>         <Button x:Name='removeButton' Content='Remove' Width='100' Height='25' Margin='10' Click='removeButton_Click'/>         <Button x:Name='replaceButton' Content='Replace' Width='100' Height='25' Margin='10' Click='replaceButton_Click'/>         <Button x:Name='resetButton' Content='Reset' Width='100' Height='25' Margin='10' Click='resetButton_Click'/>     </StackPanel> </Window> 

Next, the code behind:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Collections.ObjectModel;  namespace ObservableCollection {     /// <summary>     /// Interaction logic for Window1.xaml     /// </summary>     public partial class Window1 : Window     {         public Window1()         {             InitializeComponent();             _integerObservableCollection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_integerObservableCollection_CollectionChanged);         }          private void _integerObservableCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)         {             switch (e.Action)             {                 case System.Collections.Specialized.NotifyCollectionChangedAction.Add:                     break;                 case System.Collections.Specialized.NotifyCollectionChangedAction.Move:                     break;                 case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:                     break;                 case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:                     break;                 case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:                     break;                 default:                     break;             }         }          private void addButton_Click(object sender, RoutedEventArgs e)         {             _integerObservableCollection.Add(25);         }          private void moveButton_Click(object sender, RoutedEventArgs e)         {             _integerObservableCollection.Move(0, 19);         }          private void removeButton_Click(object sender, RoutedEventArgs e)         {             _integerObservableCollection.RemoveAt(0);         }          private void replaceButton_Click(object sender, RoutedEventArgs e)         {             _integerObservableCollection[0] = 50;         }          private void resetButton_Click(object sender, RoutedEventArgs e)         {             _integerObservableCollection.Clear();         }          private ObservableCollection<int> _integerObservableCollection = new ObservableCollection<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:32:11+00:00Added an answer on May 10, 2026 at 7:32 pm

    Ok, even though I still wish that ObservableCollection behaved as I wished … the code below is what I ended up doing. Basically, I created a new collection of T called TrulyObservableCollection and overrided the ClearItems method which I then used to raise a Clearing event.

    In the code that uses this TrulyObservableCollection, I use this Clearing event to loop through the items that are still in the collection at that point to do the detach on the event that I was wishing to detach from.

    Hope this approach helps someone else as well.

    public class TrulyObservableCollection<T> : ObservableCollection<T> {     public event EventHandler<EventArgs> Clearing;     protected virtual void OnClearing(EventArgs e)     {         if (Clearing != null)             Clearing(this, e);     }      protected override void ClearItems()     {         OnClearing(EventArgs.Empty);         base.ClearItems();     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to do something here but that error shows up i really have
i'm missing something fundamental here. i have a very simple custom class that draws
Here's something I've been thinking about: suppose you have a number, x, that can
Basic question here - I have many lines of code that look something like:
Here's the story... I have a jQuery function that does something, this function is
Here's something that's not really an issue and I apologize if it's a stupid
I'm using the BinaryFormatter.Deserialize(Stream, HeaderHandler). I may have missed something obvious here but I
I have something weird going on here. I just finished a site for a
I think I have got something simple wrong here. I am trying to pass
I have following code: public static void ProcessStep(Action action) { //do something here if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.