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

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer There's a lovely Java tutorial on that. Easy way is… May 11, 2026 at 12:44 pm
  • added an answer For others reference... You can set the FTB's ToolbarLayout property… May 11, 2026 at 12:44 pm
  • added an answer There's a TOOLSWITCH command in 9.1 but I don't think… May 11, 2026 at 12:44 pm

Related Questions

I have already posted something similar here but I would like to ask the
I have a mockup layout for something here . Essentially there are sections, columns
I have to be missing something obvious here. I don't get why this cast
I'm probably missing something very obvious here. I have files with the extension .st
I'm constantly hearing about person y had performance issue x which they solved through
I'm familiar with the LAMP stack and over the years have successfully deployed a
I've created a forum, and we're implementing an apc and memcache caching solution to
Let's say I have a set of Countries in my application. I expect this

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.