Tag: inotifycollectionchanged

集合更改事件中的System.InvalidOperationException’n’索引对于大小为’0’的集合无效

我在INotifyCollectionChanged的自定义实现上触发CollectionChanged事件时遇到此exception: PresentationFramework.dll中出现“System.InvalidOperationException”类型的exception,但未在用户代码中处理 附加信息:集合更改事件中的’25’索引对于大小为’0’的集合无效。 XAML Datagrid作为ItemsSource绑定到集合。 如何避免这种exception发生? 代码如下: public class MultiThreadObservableCollection : ObservableCollection { private readonly object lockObject; public MultiThreadObservableCollection() { lockObject = new object(); } private NotifyCollectionChangedEventHandler myPropertyChangedDelegate; public override event NotifyCollectionChangedEventHandler CollectionChanged { add { lock (this.lockObject) { myPropertyChangedDelegate += value; } } remove { lock (this.lockObject) { myPropertyChangedDelegate -= value; } } } […]

EntityFramework EntityCollection观察CollectionChanged

我首先在应用程序中使用EntityFramework数据库。 我想以某种方式通知我的ViewModel中的EntityCollection更改。 它不直接支持INotifyCollectionChanged (为什么?)并且我没有成功找到另一个解决方案。 这是我的最新尝试,由于ListChanged事件似乎没有被提升,因此无效: public class EntityCollectionObserver : ObservableCollection, INotifyCollectionChanged where T : class { public event NotifyCollectionChangedEventHandler CollectionChanged; public EntityCollectionObserver(EntityCollection entityCollection) : base(entityCollection) { IBindingList l = ((IBindingList)((IListSource)entityCollection).GetList()); l.ListChanged += new ListChangedEventHandler(OnInnerListChanged); } private void OnInnerListChanged(object sender, ListChangedEventArgs e) { if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } } 有没有人有任何想法我如何观察EntityCollection变化? 担

如何在ObservableCollection上引发CollectionChanged事件,并将更改的项目传递给它?

我有一个inheritance自ObservableCollection的类,并添加了一些其他方法,如AddRange和RemoveRange 我的基本方法调用是这样的: public void AddRange(IEnumerable collection) { foreach (var i in collection) Items.Add(i); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } 我的问题是我想访问CollectionChanged事件中的e.NewItems或e.OldItems来对CollectionChanged任何项目执行操作,并且NotifyCollectionChangedAction.Reset操作不传递这些值 void Instances_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.NewItems != null) // e.NewItems is always null { foreach (var item in e.NewItems) { if (item is EventInstanceModel) ((EventInstanceModel)item).ParentEvent = this; } } } 所以我想我可以使用NotifyCollectionChangedAction.Add而不是Reset ,但是抛出一个Range actions are not supportedexception […]

ObservableCollection元素转换/投影包装器

在WPF中创建ViewModel时,有时需要将ObservableCollection (源集合)中可用的数据转换为扩展/限制/投影原始元素(目标集合)的包装元素集合,同时将数据和顺序转换为元素始终镜像原始集合。 就像Select扩展方法一样,除了它不断更新,因此可以用于WPF绑定。 如果在索引x处将元素添加到源,则会在目标集合中的相同索引x处添加相同元素的Wrapper。 如果在源集合中删除了索引y处的元素,则会在目标集合中删除索引y处的元素。 假设有一个ObservableCollection ,但我需要绑定的是ReadOnlyObservableCollection (或等效的),其中ClassB – > ClassA如下: class ClassB : INotifyPropertyChanged, IDisposable { public ClassB(ClassA a) { Wrapped = a; (Wrapped as INotifyPropertyChanged).PropertyChanged+=WrappedChanged; } public ClassA Wrapped { get; private set; } public int SomeOtherProperty { get { return SomeFunction(Wrapped); } WrappedChanged(object s, NotifyPropertyChangedArgs a) { … } … } 我可以编写自己的TemplatedTransformCollectionWrapper […]

可观察的堆栈和队列

我正在寻找Stack和Queue的INotifyCollectionChanged实现。 我可以自己动手,但我不想重新发明轮子。