为什么订阅的 CollectionChanged 事件没有被触发?



我正在使用这个 https://stackoverflow.com/a/5256827 的插入类,该类子类ObservableCollection在集合中的项目更改时发出通知。它工作提高事件。

public sealed class TrulyObservableCollection<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
public TrulyObservableCollection()
{
CollectionChanged += FullObservableCollectionCollectionChanged;
}
public TrulyObservableCollection(IEnumerable<T> pItems) : this()
{
foreach (var item in pItems)
{
this.Add(item);
}
}
private void FullObservableCollectionCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (Object item in e.NewItems)
{
((INotifyPropertyChanged)item).PropertyChanged += ItemPropertyChanged;
}
}
if (e.OldItems != null)
{
foreach (Object item in e.OldItems)
{
((INotifyPropertyChanged)item).PropertyChanged -= ItemPropertyChanged;
}
}
}
private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{    
// Here when an item changes it works.
System.Windows.MessageBox.Show("ItemPropertyChanged fired!");        
NotifyCollectionChangedEventArgs args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, sender, sender, IndexOf((T)sender));
OnCollectionChanged(args);
}
}

当项目更改时,将触发 ItemPropertyChanged,但是当我使用该集合并将函数订阅到 OnCollectionChanged 时,不会触发订阅的事件。

public TrulyObservableCollection<ModelObj> LoadingDataCollection;


public Loading()
{            
InitializeComponent();
LoadingDataCollection = new TrulyObservableCollection<ModelObj>();
LoadingDataCollection.CollectionChanged += ContentCollectionChanged;
// Fill the collection
LoadingDataCollection = HelperClass.LoadItems();
}

public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// Never reaches here.
System.Windows.MessageBox.Show("ContentCollectionChanged fired!");
if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (ModelObj item in e.OldItems)
{
//Removed items
item.PropertyChanged -= EntityViewModelPropertyChanged;
}
}
else if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (ModelObj item in e.NewItems)
{
//Added items
item.PropertyChanged += EntityViewModelPropertyChanged;
}
}
}
public void EntityViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
//Also Never reaches here. Here I want to get the changed property and update it in the database MySQL
System.Windows.MessageBox.Show("EntityViewModelPropertyChanged");
}

知道为什么会发生这种情况吗?

编辑:已解决

正如Peter所提到的,问题是我订阅了错误的TrulyObservableCollection实例。 我没有创建新的 TrulyObservableCollection,我只是分配了 HelperClass 返回的集合。

public Loading()
{    
InitializeComponent();
// This was the wrong piece
//LoadingDataCollection = new TrulyObservableCollection<ModelObj>();
// Asign the collection returned from HelperClass
LoadingDataCollection = HelperClass.LoadItems();
// Subscribe
LoadingDataCollection.CollectionChanged += ContentCollectionChanged;
}

这段代码就在这里:

LoadingDataCollection = HelperClass.LoadItems();

正在将新集合分配给"LoadDataCollection"变量,并清除将事件处理程序分配给以前占用该变量的集合的事实。

如果将该代码更改为:

LoadingDataCollection.AddRange(HelperClass.LoadItems()); 

它应该有效。

相关内容

  • 没有找到相关文章

最新更新