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

  • Home
  • SEARCH
  • 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 6322387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:19:20+00:00 2026-05-24T16:19:20+00:00

I want a custom observablecollection so that I can catch when an item is

  • 0

I want a custom observablecollection so that I can catch when an item is added of type MyObj and call MyObj.DoCalc() on it before it’s added to Items. I also want to be able to pass in a list to myCustomCollection’s constructor, just like the base class, so that it sets items to that list.

public class MyObservableCollection : ObservableCollection<MyObj>
{

    protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
        {

            // perform calculation
        }
        base.OnCollectionChanged(e);

    }


}

That’s as far as I got. I’m not sure how to override the constructor and Items is readonly, so I don’t know how to set that to the passed in list. Thanks in advance.

  • 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. Editorial Team
    Editorial Team
    2026-05-24T16:19:22+00:00Added an answer on May 24, 2026 at 4:19 pm

    For your first question, NotifyCollectionChangedEventArgs has an NewItems property. Just loop through that.

    if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)`
    {
        foreach(var item in e.NewItems.Cast<MyObj>())
           item.DoCalc();
    }
    

    I’m not sure which order this is called.

    For your 2nd question, here is the code you need:

    public MyObservableCollection(IEnumerable<MyObj> items) : base(items)
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to generate custom methods in Eclipse like the way we can generate
I have a ComboBox that is bound to an ObservableCollection of custom UserControls. Each
I have a custom class named BlinkingLight. I also have a static ObservableCollection BlinkingLightCollection.
I want my custom type to display the stored vocabulary's title. The field definition
I've got a WPF ListBox that's databound to an ObservableCollection of type Employee. In
I am using five tab bar items in the tab bar,i want to custom
I have an ObservableCollection that is bound to a DataGrid where I want the
I want to cache custom data in an ASP.NET application. I am putting lots
I want to add custom jump list entries to my application in Windows 7.
I want to use custom exception handling, for example instead of using (Exception ex)

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.