コード例 #1
0
        /// <summary>
        /// Act when the source collection has been changed.
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The kind of modification</param>
        private void Source_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
            case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                foreach (var item in e.NewItems)
                {
                    Elements.Add(new FieldExposer <T_Item>(DataAccessor, (T_Item)item));
                }
                break;

            case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                if (e.OldItems.Count == 1)
                {
                    T_Item item  = (T_Item)e.OldItems[0];
                    var    found = Elements.Where(element => element.InnerItem.Equals(item)).FirstOrDefault();
                    if (found == null)
                    {
                        throw new InvalidOperationException("Unexpected behavior: element not in list!");
                    }
                    Elements.Remove(found);
                    var selected = SelectedElements.Where(element => element.Equals(item)).FirstOrDefault();
                    if (selected != null)
                    {
                        SelectedElements.Remove(selected);
                    }
                }
                else
                {
                    throw new InvalidOperationException("Cannot remove more than one item at once!");
                }
                break;

            default:
                Elements = new ObservableCollection <FieldExposer <T_Item> >();
                foreach (var item in (ObservableCollection <T_Item>)sender)
                {
                    Elements.Add(new FieldExposer <T_Item>(DataAccessor, (T_Item)item));
                }
                RaisePropertyChanged("Elements");
                break;
            }
        }