Force WPF to check when an event is raised

in my MVVM application I have a list of elements implementing IDataErrorInfo and INotifyPropertyChanged

view checks the objects of the virtual machine when the propertychanged event occurs.

the problem is that the result of the check depends not only on the internal state of the object, but also on the "environment", which are other objects belonging to the list.

Therefore, I need the validation to be called in all elements of the list every time an object is deleted or updated.

How can I force validaiton this way?

+4
source share
2 answers

Hi, I have an answer to your question. The default list, called ObservableCollection, raises an event only if items are added or removed. If the property change item is not raised.

You can subclass ObservableCollection and add an event for each element. There is a very good implementation in this link given by the person from StackOverFlow. ObservableCollection, which also tracks changes to items in the collection
Too good, I tried and used. It works great. Hope this helps you and guides you in the right direction!

0
source

inside the virtual machine that contains the list, each time your list changes, iterates over the list and informs each object about a change notification about a property change either on properties that, as you know, may be invalid, or only on each property by specifying a name properties for an empty string.

It looks like your check may be related to several objects, in which case you need to check from the above virtual machine and set the error messages in the corresponding objects in the collection, and then raise the property change event on these objects.

I did this earlier with the public SetErrorMessage(string string) method for objects that implement IDataErrorInfo, and the public OnPropertyChanged(string) method so that I can set errors and raise the context property changed by events outside the object.

Errors will be stored in the dictionary, and the this[string] property will look for a dictionary for errors, as well as run its own check.

+1
source

All Articles