Skip to content

Instantly share code, notes, and snippets.

@rohits79
Created June 18, 2016 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohits79/594a6e1d26527ecc679d08a74bef255c to your computer and use it in GitHub Desktop.
Save rohits79/594a6e1d26527ecc679d08a74bef255c to your computer and use it in GitHub Desktop.
public class SomeViewModel : INotifyPropertyChanged, INotifyDataErrorInfo
{
errorsContainer = new ErrorsContainer<string>(x => ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(x)));
public IEnumerable GetErrors(string propertyName)
{
return errorsContainer.GetErrors(propertyName);
}
private string propertyName;
public string PropertyName
{
get
{
return propertyName;
}
set
{
propertyName = value;
OnPropertyChanged();
}
}
private void Validate()
{
if(!someValidationLogic)
{
errorsContainer.SetErrors(()=> PropertyName, new List<string>(){"Wrong Value"});
}
else
{
errorsContainer.ClearErrors(() => PropertyName);
}
}
public bool HasErrors => errorsContainer.HasErrors;
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment