Collection properties should be read only

前端 未结 2 1038
陌清茗
陌清茗 2021-01-14 08:10

I am using FxCop for my WPF MVVM assembly and it gives me the error

Collection properties should be read only

But in my propert

相关标签:
2条回答
  • 2021-01-14 08:48

    You should rarely need to raise a PropertyChanged event on a collection. Make the collection observable so that it notifies any bindings whenever items are added or removed:

    public IList<Employee> Employees
    {
        get; 
        private set;
    }
    
    // in your constructor:
    this.Employees = new ObservableCollection<Employee>();
    
    0 讨论(0)
  • 2021-01-14 09:09

    If you make your collection an ObservableCollection then the "important" events will be when items are added and removed from the collection, not when the collection is instatiated. I agree, with FxCop. Make the collection readonly, but make it an ObservableCollection

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