Remove doen't set setter from the list?

In my C # application (MVVM template), I have something like this:

private List<MyClass> _classes= new List<MyClass>(); public List<MyClass> Classes { get { return _classes; } set { _servers = value; NotifyOfPropertyChange(() => Classes); NotifyOfPropertyChange(() => Classes2); } } public List<MyClass> Classes2 { get { return Classes.Where(class=> class.boolValue).ToList(); } } 

And when I use Classes.Add(class) or Classes.Remove(class) , setter is not called. Why?

+4
source share
2 answers

Since you do not change the Classes property, you change the internal state of the object referenced by Classes . If you need to be notified of changes to the list, you can look at the ObservableCollection :

http://msdn.microsoft.com/en-us/library/ms668604.aspx

+9
source

This is because Classes.Add does not set the Classes property, it simply calls Get , then executes the method of the returned object. You can make your setter happen by executing the Add () command and then setting the property ( Classes = Classes ) again, but a little trick. It's best to use an ObservableCollection, as Graham says, or get your own list class, which overrides the Add method to create an event.

+2
source

Source: https://habr.com/ru/post/1482390/


All Articles