ObservableCollection< T> .Move(int,int)如何工作? [英] How does ObservableCollection<T>.Move(int,int) work?
问题描述
我似乎无法阅读ObservableCollection的文档来弄清楚这一点MSDN上的.Move(int oldIndex,int newIndex):
I can't seem to figure this one out by reading the documentation for ObservableCollection.Move(int oldIndex, int newIndex) on MSDN:
oldIndex类型:System.Int32从零开始的索引指定
的位置要移动的项目。 newIndex类型:System.Int32从零开始的
索引,用于指定项目的新位置。
oldIndex Type: System.Int32 The zero-based index specifying the location of the item to be moved. newIndex Type: System.Int32 The zero-based index specifying the new location of the item.
我不了解它是如何工作的。 newIndex
的项目将如何处理?我的假设是 index> = newIndex
的每个项目的索引都会减少。这个假设正确吗?更重要的是,该行为是在MSDN上的某个地方解释还是描述的?
I don't understand how it works. What happens to the item with newIndex
? My assumption is that the index of each item with index >= newIndex
is decremented. Is that assumption correct? And more importantly, is that behavior explained or described somewhere on MSDN?
推荐答案
让我解释一下以单元测试的形式移动
:
[Test]
public void ObservableTest()
{
var observable = new ObservableCollection<string> { "A", "B", "C", "D", "E" };
observable.Move(1, 3); // oldIndex < newIndex
// Move "B" to "D"'s place: "C" and "D" are shifted left
CollectionAssert.AreEqual(new[] { "A", "C", "D", "B", "E" }, observable);
observable.Move(3, 1); // oldIndex > newIndex
// Move "B" to "C"'s place: "C" and "D" are shifted right
CollectionAssert.AreEqual(new[] { "A", "B", "C", "D", "E" }, observable);
observable.Move(1, 1); // oldIndex = newIndex
// Move "B" to "B"'s place: "nothing" happens
CollectionAssert.AreEqual(new[] { "A", "B", "C", "D", "E" }, observable);
}
这篇关于ObservableCollection< T> .Move(int,int)如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!