ObservableCollection setter isn't firing when item is added

前端 未结 1 2018
情书的邮戳
情书的邮戳 2020-12-06 14:02

I am working on a project in WPF using the MVVM Light framework. I have a DataGrid that is bound to an ObservableCollection. As of no

相关标签:
1条回答
  • 2020-12-06 14:36

    You shouldn't have public setters on lists for your objects. You should rather set ut up in your constructor

    public MyClass(){
        _masterWorkerList = new ObservableCollection<Worker>();
        _masterWorkerList.CollectionChanged += OnCollectionChanged;
    }
    
    public ObservableCollection<Worker> MasterWorkerList
    {
        get { return _masterWorkerList; }
    }
    
    private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e){
        System.Windows.MessageBox.Show("Firing");
        //RaisePropertyChanged(() => MasterWorkerList); 
    }
    

    The CollectionChanged event is called when you Add something to the ObservableCollection. If you need more ingrained control you can inherit from the ObservableCollection and override the AddItem and RemoveItem methods.

    0 讨论(0)
提交回复
热议问题