public static IEnumerable <T> RecycleCollection <T>(IList collection, IEnumerable <T> source) { // Recycle the collection of items. List <Placeholder <T> > newItems = new List <Placeholder <T> >(collection.Count); using (var recycleBin = new RecycleBin <Placeholder <T> >()) { foreach (T item in collection) { recycleBin.AddObject(new Placeholder <T>(collection, item, true)); } // Extract each item from the recycle bin. foreach (T item in source) { newItems.Add(recycleBin.Extract(new Placeholder <T>(collection, item, false))); } } for (int index = 0; index < newItems.Count; index++) { newItems[index].EnsureInCollectionAt(index); } return(newItems.Select(placeholder => placeholder.Item)); }
private void OnUpdateCollection() { // Get the source collection from the wrapped object. IEnumerable <T> sourceCollection = _getMethod(); // Create a list of new items. List <CollectionItem <T> > items = new List <CollectionItem <T> >(); // Dump all previous items into a recycle bin. using (RecycleBin <CollectionItem <T> > bin = new RecycleBin <CollectionItem <T> >()) { foreach (T oldItem in _collection) { bin.AddObject(new CollectionItem <T>(_collection, oldItem, true)); } // Add new objects to the list. if (sourceCollection != null) { foreach (T obj in sourceCollection) { items.Add(bin.Extract(new CollectionItem <T>(_collection, obj, false))); } } // All deleted items are removed from the collection at this point. } // Ensure that all items are added to the list. int index = 0; foreach (CollectionItem <T> item in items) { item.EnsureInCollection(index); ++index; } }
private void AssignToObservableCollection(List <object> newCollection) { // Create a list of new items. List <CollectionItem> items = new List <CollectionItem>(); // Dump all previous items into a recycle bin. using (RecycleBin <CollectionItem> bin = new RecycleBin <CollectionItem>()) { foreach (object oldItem in _collection) { bin.AddObject(new CollectionItem(_collection, oldItem, true)); } // Add new objects to the list. if (newCollection != null) { foreach (object obj in newCollection) { items.Add(bin.Extract(new CollectionItem(_collection, obj, false))); } } // All deleted items are removed from the collection at this point. } // Ensure that all items are added to the list. int index = 0; foreach (CollectionItem item in items) { item.EnsureInCollection(index); ++index; } }
protected internal override void PublishChanges() { _publishingChanges = true; try { if (_sourceCollection == null) { if (_collection != null) { _collection = null; _collectionChangedFromUIEventHandlerSubscribed = false; FirePropertyChanged(); } return; } if (_collection == null) { _collection = new ObservableCollection <object>(); _collection.CollectionChanged += _collectionChangedFromUIEventHandler; _collectionChangedFromUIEventHandlerSubscribed = true; FirePropertyChanged(); } // Create a list of new items. List <CollectionItem> items = new List <CollectionItem>(); // Dump all previous items into a recycle bin. using (RecycleBin <CollectionItem> bin = new RecycleBin <CollectionItem>()) { foreach (object oldItem in _collection) { bin.AddObject(new CollectionItem(_collection, oldItem, true)); } // Add new objects to the list. if (_sourceCollection != null) { foreach (object obj in _sourceCollection) { items.Add(bin.Extract(new CollectionItem(_collection, WrapValue(obj), false))); } } // All deleted items are removed from the collection at this point. } // Ensure that all items are added to the list. int index = 0; foreach (CollectionItem item in items) { item.EnsureInCollection(index); ++index; } } finally { _publishingChanges = false; } }
private void UpdateItems() { ++_updating; try { if (GetItems != null) { // Use the adapter if the check state event is not defined. GetObjectCheckStateDelegate getItemCheckState = GetItemCheckState; if (getItemCheckState == null) { getItemCheckState = new GetObjectCheckStateDelegate(AdaptGetItemCheckState); } SetObjectCheckStateDelegate setItemCheckState = SetItemCheckState; if (setItemCheckState == null) { setItemCheckState = new SetObjectCheckStateDelegate(AdaptSetItemCheckState); } // Recycle the collection of items. ArrayList newItems = new ArrayList(base.Items.Count); using (var recycleBin = new RecycleBin <CheckedListBoxItem>()) { foreach (CheckedListBoxItem item in base.Items) { recycleBin.AddObject(item); } // Extract each item from the recycle bin. foreach (object item in GetItems()) { newItems.Add(recycleBin.Extract( new CheckedListBoxItem(item, GetItemText, getItemCheckState, setItemCheckState))); } } // Replace the items in the control. base.BeginUpdate(); try { // Make sure the same tag is selected. CheckedListBoxItem selectedItem = (CheckedListBoxItem)base.SelectedItem; object selectedTag = selectedItem == null ? null : selectedItem.Tag; base.Items.Clear(); base.Items.AddRange(newItems.ToArray()); base.SelectedIndex = IndexOfTag(selectedTag); } finally { base.EndUpdate(); } } } finally { --_updating; } }
protected override void UpdateValue() { // Get the source collection from the wrapped object. IEnumerable source = ClassProperty.GetObjectValue(ObjectInstance.WrappedObject) as IEnumerable; if (source == null) { _collection = null; return; } List <object> sourceCollection = source.OfType <object>().ToList(); // Delay the update to the observable collection so that we don't record dependencies on // properties used in the items template. XAML will invoke the item template synchronously // as we add items to the observable collection, thus causing other view model property // getters to fire. _delay = delegate { if (_collection == null) { _collection = new ObservableCollection <object>(); FirePropertyChanged(); } // Create a list of new items. List <CollectionItem> items = new List <CollectionItem>(); // Dump all previous items into a recycle bin. using (RecycleBin <CollectionItem> bin = new RecycleBin <CollectionItem>()) { foreach (object oldItem in _collection) { bin.AddObject(new CollectionItem(_collection, oldItem, true)); } // Add new objects to the list. if (sourceCollection != null) { foreach (object obj in sourceCollection) { items.Add(bin.Extract(new CollectionItem(_collection, TranslateOutgoingValue(obj), false))); } } // All deleted items are removed from the collection at this point. } // Ensure that all items are added to the list. int index = 0; foreach (CollectionItem item in items) { item.EnsureInCollection(index); ++index; } }; }
public void UpdateNow() { if (_provider.IsCollection) { _depValue.OnGet(); // Create a list of new items. List <CollectionItem> items = new List <CollectionItem>(); // Dump all previous items into a recycle bin. using (RecycleBin <CollectionItem> bin = new RecycleBin <CollectionItem>()) { foreach (object oldItem in _collection) { bin.AddObject(new CollectionItem(_collection, oldItem, true)); } // Add new objects to the list. if (_sourceCollection != null) { foreach (object obj in _sourceCollection) { items.Add(bin.Extract(new CollectionItem(_collection, WrapValue(obj), false))); } } _sourceCollection = null; // All deleted items are removed from the collection at this point. } // Ensure that all items are added to the list. int index = 0; foreach (CollectionItem item in items) { item.EnsureInCollection(index); ++index; } } else { object oldValue = _value; _depValue.OnGet(); object newValue = _value; if (_initialized) { if ((!Object.Equals(newValue, oldValue))) { _wrapper.FirePropertyChanged(_propertyInfo.Name); } } else { _initialized = true; } } }
protected override void PublishChanges() { if (_sourceCollection == null) { if (_bindingList != null) { _bindingList = null; FirePropertyChanged(); } return; } if (_bindingList == null) { _bindingList = new BindingList <object>(); FirePropertyChanged(); } _updating = true; // Create a list of new items. List <CollectionItem> items = new List <CollectionItem>(); // Dump all previous items into a recycle bin. using (RecycleBin <CollectionItem> bin = new RecycleBin <CollectionItem>()) { foreach (object oldItem in _bindingList) { bin.AddObject(new CollectionItem(_bindingList, oldItem, true)); } // Add new objects to the list. if (_sourceCollection != null) { foreach (object obj in _sourceCollection) { items.Add(bin.Extract(new CollectionItem(_bindingList, WrapValue(obj), false))); } } // All deleted items are removed from the collection at this point. } // Ensure that all items are added to the list. int index = 0; foreach (CollectionItem item in items) { item.EnsureInCollection(index); ++index; } _updating = false; }
private void OnUpdateCollection() { // Get the source collection from the wrapped object. IEnumerable source = ClassProperty.GetObjectValue(ObjectInstance.WrappedObject) as IEnumerable; List<object> sourceCollection = source.OfType<object>().ToList(); // Delay the update to the observable collection so that we don't record dependencies on // properties used in the items template. XAML will invoke the item template synchronously // as we add items to the observable collection, thus causing other view model property // getters to fire. _delay = delegate { // Create a list of new items. List<CollectionItem> items = new List<CollectionItem>(); // Dump all previous items into a recycle bin. using (RecycleBin<CollectionItem> bin = new RecycleBin<CollectionItem>()) { foreach (object oldItem in _collection) bin.AddObject(new CollectionItem(_collection, oldItem, true)); // Add new objects to the list. if (sourceCollection != null) foreach (object obj in sourceCollection) items.Add(bin.Extract(new CollectionItem(_collection, TranslateOutgoingValue(obj), false))); // All deleted items are removed from the collection at this point. } // Ensure that all items are added to the list. int index = 0; foreach (CollectionItem item in items) { item.EnsureInCollection(index); ++index; } }; }
private void UpdateItems() { ++_updating; try { if ( GetItems != null ) { // Use the adapter if the check state event is not defined. GetObjectCheckStateDelegate getItemCheckState = GetItemCheckState; if ( getItemCheckState == null ) getItemCheckState = new GetObjectCheckStateDelegate( AdaptGetItemCheckState ); SetObjectCheckStateDelegate setItemCheckState = SetItemCheckState; if ( setItemCheckState == null ) setItemCheckState = new SetObjectCheckStateDelegate( AdaptSetItemCheckState ); // Recycle the collection of items. ArrayList newItems = new ArrayList( base.Items.Count ); using (var recycleBin = new RecycleBin<CheckedListBoxItem>()) { foreach (CheckedListBoxItem item in base.Items) recycleBin.AddObject(item); // Extract each item from the recycle bin. foreach ( object item in GetItems() ) { newItems.Add( recycleBin.Extract( new CheckedListBoxItem( item, GetItemText, getItemCheckState, setItemCheckState ) ) ); } } // Replace the items in the control. base.BeginUpdate(); try { // Make sure the same tag is selected. CheckedListBoxItem selectedItem = (CheckedListBoxItem)base.SelectedItem; object selectedTag = selectedItem == null ? null : selectedItem.Tag; base.Items.Clear(); base.Items.AddRange( newItems.ToArray() ); base.SelectedIndex = IndexOfTag( selectedTag ); } finally { base.EndUpdate(); } } } finally { --_updating; } }
private void AssignToObservableCollection(List<object> newCollection) { // Create a list of new items. List<CollectionItem> items = new List<CollectionItem>(); // Dump all previous items into a recycle bin. using (RecycleBin<CollectionItem> bin = new RecycleBin<CollectionItem>()) { foreach (object oldItem in _collection) bin.AddObject(new CollectionItem(_collection, oldItem, true)); // Add new objects to the list. if (newCollection != null) foreach (object obj in newCollection) items.Add(bin.Extract(new CollectionItem(_collection, obj, false))); // All deleted items are removed from the collection at this point. } // Ensure that all items are added to the list. int index = 0; foreach (CollectionItem item in items) { item.EnsureInCollection(index); ++index; } }