Class ModelBase
- Namespace
- EasySoftware.MvvmMini
- Assembly
- EasySoftware.MvvmMini.dll
Base class for bindable models with built-in validation error storage. Implements INotifyPropertyChanged, INotifyDataErrorInfo and IErrorContainer.
public abstract class ModelBase : BindableBase, IModel, INotifyPropertyChanged, INotifyDataErrorInfo, IErrorContainer
- Inheritance
-
ModelBase
- Implements
- Derived
- Inherited Members
Remarks
Object-level errors are stored under the Empty key.
Constructors
ModelBase()
Initializes a new instance of the ModelBase class with no errors.
public ModelBase()
Properties
Errors
Gets or sets all errors, keyed by property name.
public IReadOnlyDictionary<string, IEnumerable<string>> Errors { get; set; }
Property Value
Remarks
Object-level errors use the Empty key. Setting this property replaces the current errors with a copy of the given ones.
HasErrors
Gets a value indicating whether the container holds at least one error.
public virtual bool HasErrors { get; }
Property Value
Methods
AddError(string)
Adds an object-level error that is not tied to a specific property.
public void AddError(string errorMessage)
Parameters
errorMessagestringThe error message to add.
Exceptions
- ArgumentNullException
errorMessageis null or empty.
AddError(string, string)
Adds an error for the specified property and raises ErrorsChanged. A message that the property already has is not added twice.
public void AddError(string propName, string errorMessage)
Parameters
propNamestringThe property name, or null or empty for an object-level error.
errorMessagestringThe error message to add.
Exceptions
- ArgumentNullException
errorMessageis null or empty.
ClearErrors()
Removes all errors and raises ErrorsChanged for each property that had errors.
public void ClearErrors()
ClearErrors(string)
Removes all errors of the specified property and raises ErrorsChanged if it had any.
public void ClearErrors(string propName)
Parameters
CloneErrors(IErrorContainer)
Replaces the current errors with a copy of the errors of another container.
public virtual void CloneErrors(IErrorContainer other)
Parameters
otherIErrorContainerThe container to copy the errors from.
GetErrors(string)
Gets the errors of the specified property, or the object-level errors when propertyName is null or empty.
public virtual IEnumerable GetErrors(string propertyName)
Parameters
Returns
- IEnumerable
The errors of the property, or null if it has none.
RaiseErrorsChanged(string)
Raises ErrorsChanged for the specified property.
public void RaiseErrorsChanged(string propName)
Parameters
Events
ErrorsChanged
Occurs when the validation errors have changed for a property or for the entire entity.
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged