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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:43:52+00:00 2026-05-31T17:43:52+00:00

When I run the following test, I get an ArgumentOutOfRangeException: [TestClass] public class ReproduceException

  • 0

When I run the following test, I get an ArgumentOutOfRangeException:

[TestClass]
public class ReproduceException
{
    [TestMethod]
    public void Doesnt_throw_when_adding_to_grouped_collection()
    {
        var collection = new ListCollectionView(new List<Test>());
        collection.SortDescriptions.Add(new SortDescription("IsTrue", ListSortDirection.Ascending));
        collection.GroupDescriptions.Add(new PropertyGroupDescription("Name"));
        collection.AddNewItem(new Test() { Name = "Bob", IsTrue = false });
        collection.CommitNew();

    }
}

public class Test
{
    public string Name { get; set; }
    public bool IsTrue { get; set; }
}

An I get the following exception:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.ObjectModel.Collection`1.RemoveAt(Int32 index)
at System.Windows.Data.ListCollectionView.CommitNewForGrouping()
at System.Windows.Data.ListCollectionView.CommitNew()

Am I perhaps not using the AddNewItem / CommitNew in the right way?

  • 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-31T17:43:53+00:00Added an answer on May 31, 2026 at 5:43 pm

    Possible solutions:

    1) Do before adding a new item

     collection.NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning;
    

    2) Basically try out adding items before creation of the Grouping and Sorting:

    var collection = new ListCollectionView(new List<Test>());            
    collection.AddNewItem(new Test() { Name = "Bob", IsTrue = false });
    collection.CommitNew();
    
    collection.SortDescriptions.Add(new SortDescription("IsTrue", 
                                              ListSortDirection.Ascending));   
    collection.GroupDescriptions.Add(new PropertyGroupDescription("Name"));
    

    Analysis:

    After digging into the .NET Reflector, CommitNew() method has following check:

    // !!! When you've added GroupDescription this.IsGrouping becomes true!
    if (this.IsGrouping)
    {
        this.CommitNewForGrouping();
    }
    

    Since you’ve added GroupDescription it would commit for grouping:

    private void CommitNewForGrouping()
    {
        int num;   
    
        // !!! I believe it is None by default
        switch (this.NewItemPlaceholderPosition)
        {
            case NewItemPlaceholderPosition.AtBeginning:
                num = 1;
                break;
    
            case NewItemPlaceholderPosition.AtEnd:
                num = this._group.Items.Count - 2;
                break;
    
            default:
                // !!! Since you've not added groups -1 would be assigned to num
                num = this._group.Items.Count - 1;
                break;
        }
        int index = this._newItemIndex;
        object item = this.EndAddNew(false);
    
        // This method will call RemoveAt(num) where num == -1 in your case
        this._group.RemoveSpecialItem(num, item, false);
        this.ProcessCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, 
                 item, index));
    
    }
    
    internal void RemoveSpecialItem(int index, object item, bool loading)
    {
         ...
         // will fail since index always -1
         base.ProtectedItems.RemoveAt(index);
         ...
    }
    

    LCV has private method ProcessCollectionChangedWithAdjustedIndex which adjust index in different scenarios but it is not called whilst adding a new item with Grouping enables, I’m not sure why so looks like this is by design (?!) so you have manually specify placeholder AtBeginning for the new items.

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

Sidebar

Related Questions

consider the following test class: [TestClass] public class ExampleTests { [TestMethod] public void FileDoesNotExists()
I have the following unit test in my application: [TestMethod] public void Windsor_Can_Resolve_HomeController_Dependencies() {
When I run the following code: public class Test { Test(){ System.out.println(1); } {
When I attempt to run the following test in IntelliJ IDEA I get the
When I run the following code to test copying a directory, I get a
When I run the following test (built with F#2.0) I get OutOfMemoryException. It takes
I have the following attribute public class ImportStatusAttribute : ActionFilterAttribute { public override void
When I run my django test I get following errors, that are outside of
I have the following test case: include_once('../Logger.php'); class LoggerTest extends PHPUnit_Framework_TestCase { public function
I have the following test: var home = new DummyPageModel { Parent = null

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.