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 3438966
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:15:06+00:00 2026-05-18T08:15:06+00:00

Is it possible to resize an Observable Collection or perhaps restrict the max amount

  • 0

Is it possible to resize an Observable Collection or perhaps restrict the max amount of collection items? I have an ObservableCollection as a property in a View Model (using the MVVM pattern).

The view binds to the collection and I’ve attempted to hack a solution by providing an event handler that executes when a CollectionChanged event occurs. In the event handler, I trimmed the collection by removing as many items off the top of the collection as necessary.

ObservableCollection<string> items = new ObservableCollection<string>();
items.CollectionChanged += new NotifyCollectionChangedEventHandler(Items_Changed);

void Items_Changed(object sender, NotifyCollectionChangedEventArgs e)
{
    if(items.Count > 10)
    {
        int trimCount = items.Count - 10;
        for(int i = 0; i < trimCount; i++)
        {
            items.Remove(items[0]);
        }
    }
}

This event handler yields an InvalidOperationException because it doesn’t like the fact that I alter the collection during a CollectionChanged event. What should I do to keep my collection sized appropriately?

Solution:
Simon Mourier asked if I could create a new collection derived from ObservableCollection<T> and override InsertItem() and thats just what I did to have an auto-resizing ObservableCollection type.

public class MyCollection<T> : ObservableCollection<T>
{
    public int MaxCollectionSize { get; set; }

    public MyCollection(int maxCollectionSize = 0) : base()
    {
        MaxCollectionSize = maxCollectionsize;
    }

    protected override void InsertItem(int index, T item)
    {
        base.InsertItem(index, item);

        if(MaxCollectionSize > 0 && MaxCollectionSize < Count)
        {
            int trimCount = Count - MaxCollectionSize;
            for(int i=0; i<trimCount; i++)
            {
                RemoveAt(0);
            }
        }
    }
}
  • 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-18T08:15:06+00:00Added an answer on May 18, 2026 at 8:15 am

    Can you derive the ObservableCollection class and override the InsertItem method?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

if it possible to resize an image, using the CSS3 resize property ? I
Is possible resize geometric shapes on the fly using JavaScript? Example: I have a
Is it possible to resize a background image on load using javascript? I don't
Possible Duplicate: How to resize an image using PHP? Here's what I've tried: $image
Possible Duplicate: Android: How to resize a custom view programmatically? What's the best way
Does anybody have a clue, if it's possible to resize a <div> and the
I'm using cygwin but, is it possible to resize the terminal window via a
The image doesn't have to be saved, but is it still possible to resize
I have been trying to figure out if it is possible to programmatically resize
Possible Duplicate: JQuery: $(window).resize() doesn't work on Load I have the below jQuery function.

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.