当前位置: 首页>>代码示例>>C#>>正文


C# ObservableCollection.Insert方法代码示例

本文整理汇总了C#中System.Collections.ObservableCollection.Insert方法的典型用法代码示例。如果您正苦于以下问题:C# ObservableCollection.Insert方法的具体用法?C# ObservableCollection.Insert怎么用?C# ObservableCollection.Insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Collections.ObservableCollection的用法示例。


在下文中一共展示了ObservableCollection.Insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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.");
        }
开发者ID:Zim-Code,项目名称:ViewModels,代码行数:28,代码来源:Test_LabeledViewModelCollection.cs

示例2: InsertTest_Negative

        public static void InsertTest_Negative()
        {
            Guid[] anArray = { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
            ObservableCollection<Guid> collection = new ObservableCollection<Guid>(anArray);
            ReadOnlyObservableCollection<Guid> readonlyCol = new ReadOnlyObservableCollection<Guid>(collection);
            ((INotifyCollectionChanged)readonlyCol).CollectionChanged += (o, e) => { throw new ShouldNotBeInvokedException(); };

            Guid itemToInsert = Guid.NewGuid();
            int[] iArrInvalidValues = new Int32[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, Int32.MinValue };
            foreach (var index in iArrInvalidValues)
            {
                Assert.Throws<ArgumentOutOfRangeException>(() => collection.Insert(index, itemToInsert));
                Assert.Equal(anArray.Length, collection.Count);
            }

            int[] iArrLargeValues = new Int32[] { collection.Count + 1, Int32.MaxValue, Int32.MaxValue / 2, Int32.MaxValue / 10 };
            foreach (var index in iArrLargeValues)
            {
                Assert.Throws<ArgumentOutOfRangeException>(() => collection.Insert(index, itemToInsert));
                Assert.Equal(anArray.Length, collection.Count);
            }
        }
开发者ID:hitomi333,项目名称:corefx,代码行数:22,代码来源:ReadOnlyObservableCollection_EventsTests.cs

示例3: AddDefaultHeadersFooters

 internal static void AddDefaultHeadersFooters( ObservableCollection<DataTemplate> headersCollection, ObservableCollection<DataTemplate> footersCollection )
 {
   headersCollection.Insert( 0, DetailConfiguration.DefaultColumnManagerRowTemplate );
   headersCollection.Insert( 0, DetailConfiguration.DefaultHeaderSpacerTemplate );
 }
开发者ID:wangws556,项目名称:duoduo-chat,代码行数:5,代码来源:DetailConfiguration.cs

示例4: UpdateFilters

        private void UpdateFilters()
        {
            Log.Debug("Updating filters");

            if (_filterSchemes != null)
            {
                _filterSchemes.Schemes.CollectionChanged -= OnFilterSchemesCollectionChanged;
            }

            _filterSchemes = _filterSchemeManager.FilterSchemes;

            if (_filterSchemes != null)
            {
                _filterSchemes.Schemes.CollectionChanged += OnFilterSchemesCollectionChanged;
            }

            var newSchemes = new ObservableCollection<FilterScheme>();

            if (RawCollection == null)
            {
                _targetType = null;
            }
            else
            {
                _targetType = CollectionHelper.GetTargetType(RawCollection);
                if (_targetType != null)
                {
                    ((ICollection<FilterScheme>)newSchemes).AddRange((from scheme in _filterSchemes.Schemes
                                                                      where scheme.TargetType != null && _targetType.IsAssignableFromEx(scheme.TargetType)
                                                                      select scheme));
                }
            }

            newSchemes.Insert(0, NoFilterFilter);

            if (AvailableSchemes == null || !Catel.Collections.CollectionHelper.IsEqualTo(AvailableSchemes, newSchemes))
            {
                AvailableSchemes = newSchemes;

                var selectedFilter = _filterService.SelectedFilter ?? NoFilterFilter;
                SelectedFilterScheme = selectedFilter;
            }
        }
开发者ID:WildGums,项目名称:Orc.FilterBuilder,代码行数:43,代码来源:FilterBuilderViewModel.cs

示例5: InsertTest_Negative

        public static void InsertTest_Negative()
        {
            Guid[] anArray = { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
            ObservableCollection<Guid> collection = new ObservableCollection<Guid>(anArray);
            collection.CollectionChanged += (o, e) =>
            {
                Assert.True(false, "Should not have thrown collection changed event when removing items from invalid indices");
            };

            Guid itemToInsert = Guid.NewGuid();
            int[] iArrInvalidValues = new Int32[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, Int32.MinValue };
            foreach (var index in iArrInvalidValues)
            {
                Assert.Throws<ArgumentOutOfRangeException>(() => collection.Insert(index, itemToInsert));
                Assert.Equal(anArray.Length, collection.Count);
            }

            int[] iArrLargeValues = new Int32[] { collection.Count + 1, Int32.MaxValue, Int32.MaxValue / 2, Int32.MaxValue / 10 };
            foreach (var index in iArrLargeValues)
            {
                Assert.Throws<ArgumentOutOfRangeException>(() => collection.Insert(index, itemToInsert));
                Assert.Equal(anArray.Length, collection.Count);
            }
        }
开发者ID:johnhhm,项目名称:corefx,代码行数:24,代码来源:ObservableCollection_MethodsTest.cs

示例6: InsertTest

        public static void InsertTest()
        {
            Guid[] anArray = { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
            ObservableCollection<Guid> col0 = new ObservableCollection<Guid>((IEnumerable<Guid>)anArray);
            ObservableCollection<Guid> col1 = new ObservableCollection<Guid>((IEnumerable<Guid>)anArray);
            ObservableCollection<Guid> col3 = new ObservableCollection<Guid>((IEnumerable<Guid>)anArray);

            //inserting item at the beginning.
            Guid g0 = Guid.NewGuid();
            col0.Insert(0, g0);
            Assert.Equal(g0, col0[0]);

            // inserting item in the middle
            Guid g1 = Guid.NewGuid();
            col1.Insert(1, g1);
            Assert.Equal(g1, col1[1]);

            // inserting item at the end.
            Guid g3 = Guid.NewGuid();
            col3.Insert(col3.Count, g3);
            Assert.Equal(g3, col3[col3.Count - 1]);

            string[] anArrayString = new string[] { "one", "two", "three", "four" };
            ObservableCollection<string> collection = new ObservableCollection<string>((IEnumerable<string>)anArrayString);
            CollectionAndPropertyChangedTester helper = new CollectionAndPropertyChangedTester();
            helper.AddOrInsertItemTest(collection, "seven", 2);
            helper.AddOrInsertItemTest(collection, "zero", 0);
            helper.AddOrInsertItemTest(collection, "eight", collection.Count);
        }
开发者ID:johnhhm,项目名称:corefx,代码行数:29,代码来源:ObservableCollection_MethodsTest.cs

示例7: InsertItemBeforeSelection

		public void InsertItemBeforeSelection ()
		{
			var index = 2;
			var item = Items [index];
			var incc = new ObservableCollection<object>(Items);
			SetSource(incc);

			View.MoveCurrentToPosition(index);
			ResetCounters();
			incc.Insert(0, new object());

			Assert.AreEqual(index + 1, View.CurrentPosition, "#1");
			Assert.AreEqual(item, View.CurrentItem, "#2");
			Assert.AreEqual(0, CurrentChanged, "#3");
			Assert.AreEqual(0, CurrentChanging, "#4");
		}
开发者ID:kangaroo,项目名称:moon,代码行数:16,代码来源:CollectionViewTest.cs

示例8: ObservableCollectionTest

        //ObservableCollectionTest
        public static void ObservableCollectionTest()
        {
            var data = new ObservableCollection<string>();

            data.CollectionChanged += new NotifyCollectionChangedEventHandler(DataCollectionChanged);

            data.Add("One");
            data.Add("Two");
            data.Add("Three");
            data.Insert(1,"$^^$");
            data.RemoveAt(1);
            foreach (var tmp in data)
            {
                Console.WriteLine(tmp);
            }
        }
开发者ID:vjzr-Phonex,项目名称:C_Sharp,代码行数:17,代码来源:Program.cs

示例9: InsertLayoutDefinition

 void InsertLayoutDefinition(ObservableCollection<LayoutDefinition> list, LayoutDefinition layoutDef)
 {
     list.Insert(FindInsertIndexForDocumentFactory(list, layoutDef.DocumentFactoryName), layoutDef);
 }
开发者ID:UnaNancyOwen,项目名称:Kinect-Studio-Sample,代码行数:4,代码来源:ViewLayoutEditor.cs

示例10: AddProperty

        // ###################################################
        // CIDER-SPECIFIC CHANGE IN NEED OF PORTING - BEGIN
        // ###################################################

        // This method used to be non-virtual, private
        protected virtual void AddProperty(PropertyEntry property, ObservableCollection<PropertyEntry> unconsumedProperties, ObservableCollection<PropertyEntry> referenceOrder, ObservableCollection<CategoryEditor> categoryEditors)
        {

            // ###################################################
            // CIDER-SPECIFIC CHANGE IN NEED OF PORTING - END
            // ###################################################

            bool consumed = false;

            foreach (CategoryEditor categoryEditor in categoryEditors)
            {
                if (categoryEditor.ConsumesProperty(property))
                {
                    consumed = true;
                }
            }
            if (!consumed)
            {
                // We need to insert this property in the correct location.  Reference order is sorted and contains all properties in the unconsumed properties collection.
                Fx.Assert(referenceOrder.Contains(property), "Reference order should contain the property to be added.");
#if DEBUG
                foreach (PropertyEntry unconsumedProperty in unconsumedProperties)
                {
                    Fx.Assert(referenceOrder.Contains(unconsumedProperty), "Reference order should contain all unconsumed properties.");
                }
#endif

                // We'll walk both collections, and advance the insertion index whenever we see an unconsumed property come ahead of the target in the reference order.
                int referenceIndex = 0;
                int insertionIndex = 0;
                while (referenceOrder[referenceIndex] != property && insertionIndex < unconsumedProperties.Count)
                {
                    if (unconsumedProperties[insertionIndex] == referenceOrder[referenceIndex])
                    {
                        insertionIndex++;
                    }
                    referenceIndex++;
                }
                unconsumedProperties.Insert(insertionIndex, property);
            }
        }
开发者ID:iskiselev,项目名称:JSIL.NetFramework,代码行数:46,代码来源:CategoryContainer.xaml.cs

示例11: SetItemsSourceAndModifyCollection

        public void SetItemsSourceAndModifyCollection()
        {
            // Create the DataGrid and setup its binding
            ObservableCollection<string> strings = new ObservableCollection<string>() { "one", "two", "three" };
            bool isLoaded = false;
            DataGrid dataGrid = new DataGrid();
            dataGrid.Loaded += delegate { isLoaded = true; };

            // Set the ItemsSource and verify the slot counts
            dataGrid.ItemsSource = strings;
            dataGrid.SelectedIndex = 0;
            Assert.AreEqual(3, dataGrid.SlotCount, "Incorrect SlotCount after setting ItemsSource");
            Assert.AreEqual(3, dataGrid.VisibleSlotCount, "Incorrect VisibleSlotCount after setting ItemsSource");
            Assert.AreEqual(0, dataGrid.SelectedIndex, "SelectedIndex incorrect");

            // Remove an item and verify that the DataGrid is updated
            strings.RemoveAt(1);
            Assert.AreEqual(2, dataGrid.SlotCount, "Incorrect SlotCount after removing an item");
            Assert.AreEqual(2, dataGrid.VisibleSlotCount, "Incorrect VisibleSlotCount after removing an item");
            Assert.AreEqual(0, dataGrid.SelectedIndex, "SelectedIndex incorrect after removing an item");

            // Add an item and verify that the DataGrid is updated
            strings.Add("four");
            Assert.AreEqual(3, dataGrid.SlotCount, "Incorrect SlotCount after adding an item");
            Assert.AreEqual(3, dataGrid.VisibleSlotCount, "Incorrect VisibleSlotCount after adding an item");
            Assert.AreEqual(0, dataGrid.SelectedIndex, "SelectedIndex incorrect after adding an item");

            // Insert an item an dverify that the DataGrid is updated
            strings.Insert(0, "zero");
            Assert.AreEqual(4, dataGrid.SlotCount, "Incorrect SlotCount after inserting an item");
            Assert.AreEqual(4, dataGrid.VisibleSlotCount, "Incorrect VisibleSlotCount after inserting an item");
            Assert.AreEqual(1, dataGrid.SelectedIndex, "SelectedIndex incorrect after inserting an item");

            // Add the DataGrid to the visual tree
            TestPanel.Children.Add(dataGrid);
            this.EnqueueConditional(delegate { return isLoaded; });

            this.EnqueueYieldThread();
            this.EnqueueCallback(delegate
            {
                Assert.AreEqual(4, dataGrid.SlotCount, "Incorrect SlotCount after adding and removing items");
                Assert.AreEqual(4, dataGrid.VisibleSlotCount, "Incorrect VisibleSlotCount after adding and removing items");
                Assert.AreEqual(1, dataGrid.SelectedIndex, "SelectedIndex was not updated when collection changed");
                Assert.AreEqual("one", dataGrid.SelectedItem, "SelectedItem was not updated when collection changed");

                DataGridRow row = dataGrid.DisplayData.GetDisplayedRow(0);
                Assert.IsNotNull(row);
                Assert.AreEqual(0, row.Slot, "Incorrect Row.Slot");
                Assert.AreEqual(0, row.Index, "Incorrect Row.Index");
                Assert.AreEqual("zero", row.DataContext, "Incorrect Row.DataContext");

                row = dataGrid.DisplayData.GetDisplayedRow(1);
                Assert.IsNotNull(row);
                Assert.AreEqual(1, row.Slot, "Incorrect Row.Slot");
                Assert.AreEqual(1, row.Index, "Incorrect Row.Index");
                Assert.AreEqual("one", row.DataContext, "Incorrect Row.DataContext");

                row = dataGrid.DisplayData.GetDisplayedRow(2);
                Assert.IsNotNull(row);
                Assert.AreEqual(2, row.Slot, "Incorrect Row.Slot");
                Assert.AreEqual(2, row.Index, "Incorrect Row.Index");
                Assert.AreEqual("three", row.DataContext, "Incorrect Row.DataContext");

                row = dataGrid.DisplayData.GetDisplayedRow(3);
                Assert.IsNotNull(row);
                Assert.AreEqual(3, row.Slot, "Incorrect Row.Slot");
                Assert.AreEqual(3, row.Index, "Incorrect Row.Index");
                Assert.AreEqual("four", row.DataContext, "Incorrect Row.DataContext");
            });
            this.EnqueueTestComplete();
        }
开发者ID:kvervo,项目名称:HorizontalLoopingSelector,代码行数:71,代码来源:ItemsSource.cs

示例12: 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)); 
            }
        } 
开发者ID:sjyanxin,项目名称:WPFSource,代码行数:93,代码来源:ItemCollection.cs

示例13: LoadSubSidiary

 /// <summary>
 /// 
 /// </summary>
 public void LoadSubSidiary()
 {
     if (KmtConstants.IsOemCorp || KmtConstants.IsTpiCorp)
     {
         Subsidiarys = new ObservableCollection<Subsidiary>(ssProxy.GetSubsidiaries());
         Subsidiarys.Insert(0, new Subsidiary() { SsId = 0, DisplayName = MergedResources.Common_All });
         //Select 'Show All' on First Load
         SelectedSubSidiary = Subsidiarys.FirstOrDefault();
     }
     else// Factory Floor
         Subsidiarys = new ObservableCollection<Subsidiary>();
 }
开发者ID:barbarossia,项目名称:DIS,代码行数:15,代码来源:KeyManagementViewModel.cs

示例14: MakeRootDesigner

        internal void MakeRootDesigner(ModelItem modelItem, bool setAsSelection, bool checkIfCanBeMadeRoot)
        {
            ModelItem currentRootModelItem = (this.RootDesigner != null) ? ((WorkflowViewElement)this.RootDesigner).ModelItem : null;
            if (modelItem == currentRootModelItem)
            {
                return;
            }
            if (typeof(ActivityBuilder).IsAssignableFrom(modelItem.ItemType))
            {
                this.ActivitySchema = modelItem;
            }

            WorkflowViewService viewService = this.Context.Services.GetService<ViewService>() as WorkflowViewService;

            //try get designer for given model item
            Type designerType = viewService.GetDesignerType(modelItem.ItemType);
            //if one doesn't exist - check its parent tree, perhaps there will be one
            while (null == designerType && null != modelItem.Parent)
            {
                modelItem = modelItem.Parent;
                designerType = viewService.GetDesignerType(modelItem.ItemType);
            }

            if (viewService.ShouldAppearOnBreadCrumb(modelItem, checkIfCanBeMadeRoot))
            {
                UpdateAncestorFlag(currentRootModelItem, modelItem);
                Dictionary<ModelItem, ModelItem> newSelectionMap = new Dictionary<ModelItem, ModelItem>();
                ModelItem newRootModelItem = modelItem;
                ObservableCollection<object> breadCrumbCollection = new ObservableCollection<object>();
                object breadCrumbObjectConnector = null;
                bool isFirstAdded = false;
                while (modelItem != null)
                {
                    bool shouldCheckIfCanBeMadeRoot = true;
                    if (isFirstAdded)
                    {
                        shouldCheckIfCanBeMadeRoot = checkIfCanBeMadeRoot;
                    }
                    if (viewService.ShouldAppearOnBreadCrumb(modelItem, shouldCheckIfCanBeMadeRoot))
                    {
                        if (isFirstAdded)
                        {
                            breadCrumbObjectConnector = new BreadCrumbObjectSeparator();
                            breadCrumbCollection.Insert(0, breadCrumbObjectConnector);
                        }
                        breadCrumbCollection.Insert(0, modelItem);
                        isFirstAdded = true;
                        if (selectionMap.ContainsKey(modelItem))
                        {
                            newSelectionMap.Add(modelItem, selectionMap[modelItem]);
                        }
                    }
                    modelItem = modelItem.Parent;
                }

                //Remember the selection for the current root.
                WorkflowViewElement focusedElement = Keyboard.FocusedElement as WorkflowViewElement;
                //This condition will be true when we are breadcrumbing into a child element.
                if (focusedElement != null && object.Equals(focusedElement.ModelItem, newRootModelItem))
                {
                    if (currentRootModelItem != null)
                    {
                        newSelectionMap[currentRootModelItem] = newRootModelItem;
                    }
                }
                this.selectionMap = newSelectionMap;
                SetAsRootDesignerView(newRootModelItem, setAsSelection);
                breadCrumbListBox.ItemsSource = breadCrumbCollection;
                // Move to the top left so that the display name is visible.
                this.ScrollViewer.ScrollToTop();
                this.ScrollViewer.ScrollToLeftEnd();
            }
        }
开发者ID:iskiselev,项目名称:JSIL.NetFramework,代码行数:73,代码来源:DesignerView.xaml.cs

示例15: AddOrInsertItemTest

        /// <summary>
        /// Will perform an Add or Insert on the given Collection depending on whether the 
        /// insertIndex is null or not. If it is null, will Add, otherwise, will Insert.
        /// </summary>
        public void AddOrInsertItemTest(ReadOnlyObservableCollection<string> readOnlyCol, ObservableCollection<string> collection,
            string itemToAdd, int? insertIndex = null)
        {
            INotifyPropertyChanged readOnlyPropertyChanged = readOnlyCol;
            readOnlyPropertyChanged.PropertyChanged += Collection_PropertyChanged;
            _expectedPropertyChanged = new[]
            {
                new PropertyNameExpected(COUNT),
                new PropertyNameExpected(ITEMARRAY)
            };

            INotifyCollectionChanged readOnlyCollectionChanged = readOnlyCol;
            readOnlyCollectionChanged.CollectionChanged += Collection_CollectionChanged;
            _expectedCollectionChangedFired++;
            _expectedAction = NotifyCollectionChangedAction.Add;
            _expectedNewItems = new string[] { itemToAdd };
            if (insertIndex.HasValue)
                _expectedNewStartingIndex = insertIndex.Value;
            else
                _expectedNewStartingIndex = collection.Count;
            _expectedOldItems = null;
            _expectedOldStartingIndex = -1;

            int expectedCount = collection.Count + 1;

            if (insertIndex.HasValue)
            {
                collection.Insert(insertIndex.Value, itemToAdd);
                Assert.Equal(itemToAdd, readOnlyCol[insertIndex.Value]);
            }
            else
            {
                collection.Add(itemToAdd);
                Assert.Equal(itemToAdd, readOnlyCol[collection.Count - 1]);
            }

            Assert.Equal(expectedCount, readOnlyCol.Count);
            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 added an item");

            readOnlyCollectionChanged.CollectionChanged -= Collection_CollectionChanged;
            readOnlyPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
        }
开发者ID:hitomi333,项目名称:corefx,代码行数:49,代码来源:ReadOnlyObservableCollection_EventsTests.cs


注:本文中的System.Collections.ObservableCollection.Insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。