I am working setting up INotifyDataErrorInfo on a view model, to handle validation with attributes.
I have it working fine in the UI, the text box gets a nice red boarder and the mouse over event says what is wrong.
But I can not work out how in the ViewModel to work out the view model is valid. I am guessing I have to set up the HasErrors. In the examples I have seen they have a variable
private Dictionary<string, List<string>> _PropertyErrors = new Dictionary<string, List<string>>();
But then do nothing to set it.
I would like to check in the Save() method if the view model is valid.
public class CustomerViewModel : EntityBase, INotifyPropertyChanged, INotifyDataErrorInfo
{
public CustomerViewModel ()
{
//SET UP
}
private string _HomePhone;
[Required]
public string HomePhone
{
get { return _HomePhone; }
set
{
if (_HomePhone != value)
{
_HomePhone = value;
PropertyChanged(this, new PropertyChangedEventArgs("HomePhone"));
}
}
}
private void Save()
{
//Break point here
}
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public bool HasErrors
{
get { return true; }
}
public IEnumerable GetErrors(string propertyName)
{
return null;
}
You can check the
HasErrorsproperty.This is an example implementation of
INotifyDataErrorInfowithValidationAttributesupport and providing an exampleTrySave(), which checks if the view model has any validation errors:This link contains an explanation and links to more examples: How to add validation to view model properties or how to implement INotifyDataErrorInfo