0

リロードボタンで置き換える必要がある 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#ではオーバーライドできないため、上記の動作を説明する方法がわかりません...誰もこれを知っていますか? ?

4

1 に答える 1

0

(コメントより)

コレクションを「埋めている」人は誰でも、古い「コレクション」にまだ接続されているのではないかと思います

つまりINotifyCollectionChanged, that's just for inside collection、「サブスクライバー」に「全体」コレクションが変更されたこと、および INotifyPropertyChanged を使用して「通知」する必要がありますyou need to call OnPropertyChanged("Collection") or whatever is named

于 2013-03-31T19:48:11.350 に答える