リロードボタンで置き換える必要がある ObservableCollection があります。これを試しているうちに、変数「myCollection」が「ReLoadData」で無効にされていても CollectionChanged イベントが発生することがわかり (以下のコード例を参照)、CollectionChanged メンバーにイベント ハンドラーを追加していない新しい ObservableCollection を割り当てました。
public partial class MainWindow : Window
{
private ObservableCollection<string> myCollection =
new ObservableCollection<string>();
public MainWindow()
{
InitializeComponent();
myCollection.CollectionChanged += new
System.Collections.Specialized.NotifyCollectionChangedEventHandler(
myCollection_CollectionChanged);
}
//Invoked in button click handler:
private void ReLoadData()
{
ObservableCollection<string> newCollection =
new ObservableCollection<string>();
//Filling newCollection with stuff...
//Marks old collection for the garbage collector
myCollection = null;
myCollection = newCollection;
}
void myCollection_CollectionChanged(
object sender,
System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
//Set breakpoint on statement or one of the braces.
}
private void AddItem(object sender, RoutedEventArgs args)
{
//Why does the following fire CollectionChanged
//although myCollection was nullified in
//ReLoadAuctionData() before?
myCollection.Add("AddedItem");
}
}
これは、代入演算子がC#でどのように実装されているかに関係していると思われますが、私が読んだ限りでは、C#ではオーバーライドできないため、上記の動作を説明する方法がわかりません...誰もこれを知っていますか? ?