本文整理汇总了C#中System.Collections.ObservableCollection.Move方法的典型用法代码示例。如果您正苦于以下问题:C# ObservableCollection.Move方法的具体用法?C# ObservableCollection.Move怎么用?C# ObservableCollection.Move使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.ObservableCollection
的用法示例。
在下文中一共展示了ObservableCollection.Move方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestCollectionSync
public void TestCollectionSync()
{
string item0 = "Item0";
string item1 = "Item1";
string item2 = "Item2";
string item3 = "Item3";
ObservableCollection<string> collection = new ObservableCollection<string>();
HelperLabeledViewModelCollection viewModel = new HelperLabeledViewModelCollection(null, collection, o => o);
collection.Add(item0);
collection.Add(item1);
collection.Add(item3);
Assert.IsTrue(CompareCollectionValues(collection, viewModel), "Add did not work.");
collection.Insert(2, item2);
Assert.IsTrue(CompareCollectionValues(collection, viewModel), "Insert did not work.");
collection.Remove(item3);
Assert.IsTrue(CompareCollectionValues(collection, viewModel), "Remove did not work.");
collection.Move(0, 1);
Assert.IsTrue(CompareCollectionValues(collection, viewModel), "Move did not work.");
collection.Clear();
Assert.IsTrue(CompareCollectionValues(collection, viewModel), "Clear did not work.");
}
示例2: TestWindowViewModel
public TestWindowViewModel()
{
RWLock = new ReaderWriterLockSlim();
Observable = new ObservableCollection<int>();
BindingOperations.EnableCollectionSynchronization(Observable, RWLock, new CollectionSynchronizationCallback(lockCollection));
AddCommand = new Command(() =>
{
Task.Factory.StartNew(() =>
{
RWLock.EnterWriteLock();
for (int i = 0; i < 10; i++)
{
Observable.Add(i);
}
RWLock.ExitWriteLock();
});
});
ClearCommand = new Command(() =>
{
Task.Factory.StartNew(() =>
{
RWLock.EnterWriteLock();
Observable.Move(0,5);
RWLock.ExitWriteLock();
int k = 0;
for (int i = 0; i < 1000; i++)
{
k++;
}
});
});
}
示例3: MoveItemTest
/// <summary>
/// Given a collection, will move an item from the oldIndex to the newIndex.
/// </summary>
public void MoveItemTest(ReadOnlyObservableCollection<string> readOnlyCol, ObservableCollection<string> collection,
int oldIndex, int newIndex)
{
INotifyPropertyChanged readOnlyPropertyChanged = readOnlyCol;
readOnlyPropertyChanged.PropertyChanged += Collection_PropertyChanged;
_expectedPropertyChanged = new[] { new PropertyNameExpected(ITEMARRAY) };
INotifyCollectionChanged readOnlyCollectionChange = readOnlyCol;
readOnlyCollectionChange.CollectionChanged += Collection_CollectionChanged;
string itemAtOldIndex = collection[oldIndex];
_expectedCollectionChangedFired++;
_expectedAction = NotifyCollectionChangedAction.Move;
_expectedNewItems = new string[] { itemAtOldIndex };
_expectedNewStartingIndex = newIndex;
_expectedOldItems = new string[] { itemAtOldIndex };
_expectedOldStartingIndex = oldIndex;
collection.Move(oldIndex, newIndex);
Assert.Equal(collection.Count, readOnlyCol.Count);
Assert.Equal(itemAtOldIndex, readOnlyCol[newIndex]);
Assert.Equal(_expectedCollectionChangedFired, _numCollectionChangedFired);
foreach (var item in _expectedPropertyChanged)
Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since we moved an item.");
readOnlyCollectionChange.CollectionChanged -= Collection_CollectionChanged;
readOnlyPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
}
示例4: MoveTest_Negative
public static void MoveTest_Negative()
{
string[] anArray = new string[] { "one", "two", "three", "four" };
ObservableCollection<string> collection = new ObservableCollection<string>(anArray);
ReadOnlyObservableCollection<string> readonlyCol = new ReadOnlyObservableCollection<string>(collection);
((INotifyCollectionChanged)readonlyCol).CollectionChanged += (o, e) => { throw new ShouldNotBeInvokedException(); };
int validIndex = 2;
int[] iArrInvalidValues = new Int32[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, Int32.MinValue };
int[] iArrLargeValues = new Int32[] { anArray.Length, Int32.MaxValue, Int32.MaxValue / 2, Int32.MaxValue / 10 };
foreach (var index in iArrInvalidValues)
{
// invalid startIndex, valid destination index.
Assert.Throws<ArgumentOutOfRangeException>(() => collection.Move(index, validIndex));
Assert.Equal(anArray.Length, collection.Count);
}
foreach (var index in iArrLargeValues)
{
// invalid startIndex, valid destination index.
Assert.Throws<ArgumentOutOfRangeException>(() => collection.Move(index, validIndex));
Assert.Equal(anArray.Length, collection.Count);
}
}
示例5: MoveItemTest
/// <summary>
/// Given a collection, will move an item from the oldIndex to the newIndex.
/// </summary>
public void MoveItemTest(ObservableCollection<string> collection, int oldIndex, int newIndex)
{
INotifyPropertyChanged collectionPropertyChanged = collection;
collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged;
_expectedPropertyChanged = new[] { new PropertyNameExpected(ITEMARRAY) };
collection.CollectionChanged += Collection_CollectionChanged;
string itemAtOldIndex = collection[oldIndex];
ExpectedCollectionChangedFired++;
ExpectedAction = NotifyCollectionChangedAction.Move;
ExpectedNewItems = new string[] { itemAtOldIndex };
ExpectedNewStartingIndex = newIndex;
ExpectedOldItems = new string[] { itemAtOldIndex };
ExpectedOldStartingIndex = oldIndex;
int expectedCount = collection.Count;
collection.Move(oldIndex, newIndex);
Assert.Equal(expectedCount, collection.Count);
Assert.Equal(itemAtOldIndex, collection[newIndex]);
Assert.Equal(ExpectedCollectionChangedFired, NumCollectionChangedFired);
foreach (var item in _expectedPropertyChanged)
Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since we just moved an item");
collection.CollectionChanged -= Collection_CollectionChanged;
collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
}
示例6: MoveTest_Negative
public static void MoveTest_Negative()
{
string[] anArray = new string[] { "one", "two", "three", "four" };
ObservableCollection<string> collection = null;
int validIndex = 2;
int[] iArrInvalidValues = new Int32[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, Int32.MinValue };
int[] iArrLargeValues = new Int32[] { anArray.Length, Int32.MaxValue, Int32.MaxValue / 2, Int32.MaxValue / 10 };
foreach (var index in iArrInvalidValues)
{
collection = new ObservableCollection<string>(anArray);
collection.CollectionChanged += (o, e) =>
{
Assert.True(false, "Should not have thrown collection changed event when removing items from invalid indices");
};
// invalid startIndex, valid destination index.
Assert.Throws<ArgumentOutOfRangeException>(() => collection.Move(index, validIndex));
Assert.Equal(anArray.Length, collection.Count);
// valid startIndex, invalid destIndex.
Assert.Throws<ArgumentOutOfRangeException>(() => collection.Move(validIndex, index));
//NOTE: It actually moves the item right out of the collection.So the count is one less.
//Assert.Equal(anArray.Length, collection.Count, "Collection should not have changed. index: " + index);
}
foreach (var index in iArrLargeValues)
{
collection = new ObservableCollection<string>(anArray);
collection.CollectionChanged += (o, e) =>
{
Assert.True(false, "Should not have thrown collection changed event when removing items from invalid indices");
};
// invalid startIndex, valid destination index.
Assert.Throws<ArgumentOutOfRangeException>(() => collection.Move(index, validIndex));
Assert.Equal(anArray.Length, collection.Count);
// valid startIndex, invalid destIndex.
Assert.Throws<ArgumentOutOfRangeException>(() => collection.Move(validIndex, index));
//NOTE: It actually moves the item right out of the collection. So the count is one less.
//Assert.Equal(anArray.Length, collection.Count, "Collection should not have changed.");
}
}
示例7: MoveTest
public static void MoveTest()
{
Guid[] anArray = { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
ObservableCollection<Guid> col01 = new ObservableCollection<Guid>((IEnumerable<Guid>)anArray);
ObservableCollection<Guid> col10 = new ObservableCollection<Guid>((IEnumerable<Guid>)anArray);
ObservableCollection<Guid> col12 = new ObservableCollection<Guid>((IEnumerable<Guid>)anArray);
ObservableCollection<Guid> col21 = new ObservableCollection<Guid>((IEnumerable<Guid>)anArray);
ObservableCollection<Guid> col20 = new ObservableCollection<Guid>((IEnumerable<Guid>)anArray);
col01.Move(0, 1);
Assert.Equal(anArray[0], col01[1]);
col10.Move(1, 0);
Assert.Equal(anArray[1], col10[0]);
col12.Move(1, 2);
Assert.Equal(anArray[1], col12[2]);
col21.Move(2, 1);
Assert.Equal(anArray[2], col21[1]);
col20.Move(2, 0);
Assert.Equal(anArray[2], col20[0]);
CollectionAndPropertyChangedTester helper = new CollectionAndPropertyChangedTester();
string[] anArrayString = new string[] { "one", "two", "three", "four" };
ObservableCollection<string> collection = new ObservableCollection<string>(anArrayString);
helper.MoveItemTest(collection, 0, 2);
helper.MoveItemTest(collection, 3, 0);
helper.MoveItemTest(collection, 1, 2);
}
示例8: SynchronizeGroupDescriptions
// keep inner and outer CollViews' GroupDescription collections in synch
private void SynchronizeGroupDescriptions(NotifyCollectionChangedEventArgs e, ObservableCollection<GroupDescription> origin, ObservableCollection<GroupDescription> clone)
{
if (clone == null)
return; // the clone might be lazily-created _groupBy
int i;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
Debug.Assert(e.NewStartingIndex >= 0);
if (clone.Count + e.NewItems.Count != origin.Count)
goto case NotifyCollectionChangedAction.Reset;
for (i = 0; i < e.NewItems.Count; i++)
{
clone.Insert(e.NewStartingIndex + i, (GroupDescription) e.NewItems[i]);
}
break;
case NotifyCollectionChangedAction.Remove:
Debug.Assert(e.OldStartingIndex >= 0);
if (clone.Count - e.OldItems.Count != origin.Count)
goto case NotifyCollectionChangedAction.Reset;
for (i = 0; i < e.OldItems.Count; i++)
{
clone.RemoveAt(e.OldStartingIndex);
}
break;
case NotifyCollectionChangedAction.Replace:
Debug.Assert(e.OldStartingIndex >= 0);
if (clone.Count + e.NewItems.Count - e.OldItems.Count != origin.Count)
goto case NotifyCollectionChangedAction.Reset;
// If there are as many new items as old items, then
// this is a straight replace.
if (e.OldItems.Count == e.NewItems.Count)
{
for (i = 0; i < e.OldItems.Count; i++)
{
clone[e.OldStartingIndex + i] = (GroupDescription) e.NewItems[i];
}
}
else
{
for (i = 0; i < e.OldItems.Count; i++)
{
clone.RemoveAt(e.OldStartingIndex);
}
for (i = 0; i < e.NewItems.Count; i++)
{
clone.Insert(e.NewStartingIndex + i, (GroupDescription) e.NewItems[i]);
}
}
break;
case NotifyCollectionChangedAction.Move:
Debug.Assert(e.OldStartingIndex >= 0);
if (clone.Count != origin.Count)
goto case NotifyCollectionChangedAction.Reset;
if (e.OldItems.Count == 1)
{
clone.Move(e.OldStartingIndex, e.NewStartingIndex);
}
else
{
if (e.NewStartingIndex < e.OldStartingIndex)
{
for (i = 0; i < e.OldItems.Count; i++)
{
clone.Move(e.OldStartingIndex + i, e.NewStartingIndex + i);
}
}
else if (e.NewStartingIndex > e.OldStartingIndex)
{
for (i = e.OldItems.Count - 1; i >= 0; i--)
{
clone.Move(e.OldStartingIndex + i, e.NewStartingIndex + i);
}
}
}
break;
// this arm also handles cases where the two collections have gotten
// out of [....] (typically because exceptions prevented a previous [....]
// from happening)
case NotifyCollectionChangedAction.Reset:
CloneList(clone, origin);
break;
default:
throw new NotSupportedException(SR.Get(SRID.UnexpectedCollectionChangeAction, e.Action));
}
}