有ListBox一個
listBox.ItemsSource = myObservableCollection;
將事件處理程式添加到myObservableCollection.CollectionChanged.
但
listBox.ItemsSource = null;
要么
listBox.ItemsSource = anotherObservableCollection;
不會從myObservableCollection.CollectionChanged.
如何ListBox從該事件中洗掉其事件處理程式?
可以看到事件處理程式實際上沒有被洗掉,例如在使用這樣的東西時:
class MyObservableCollection<T> : ObservableCollection<T>
{
public override event NotifyCollectionChangedEventHandler? CollectionChanged
{
add { Trace.WriteLine("add"); base.CollectionChanged = value; }
remove { Trace.WriteLine("remove"); base.CollectionChanged -= value; }
}
}
uj5u.com熱心網友回復:
如何使 ListBox 從該事件中洗掉其事件處理程式?
使用以下DetachFromSourceCollection方法CollectionView:
CollectionView view = CollectionViewSource.GetDefaultView(listBox.ItemsSource)
as CollectionView;
view?.DetachFromSourceCollection();
listBox.ItemsSource = null;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/446783.html
下一篇:默認禁用.NET6功能
