gpt4 book ai didi

c# - 将项目添加到 ListCollectionView 时抛出异常

转载 作者:太空宇宙 更新时间:2023-11-03 14:04:22 24 4
gpt4 key购买 nike

当我运行以下测试时,我得到一个 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; }
}

我得到以下异常:

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()

我可能没有以正确的方式使用 AddNewItem/CommitNew 吗?

最佳答案

可能的解决方案:

1)在添加新项目之前做

 collection.NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning;

2) 基本上尝试在创建分组和排序之前添加项目:

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"));

分析:

深入研究 .NET Reflector 后,CommitNew() 方法有以下检查:

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

由于您已经添加了 GroupDescription,它将提交分组:

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 有私有(private)方法 ProcessCollectionChangedWithAdjustedIndex 可以在不同情况下调整索引,但在添加启用分组的新项目时不会调用它,我不确定为什么看起来像这样是设计使然(? !) 因此您必须为新项目手动指定占位符 AtBeginning

关于c# - 将项目添加到 ListCollectionView 时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9836757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com