ModelDataError Class

Base class for models that required the INotifyDataErrorInfo and INotifyPropertyChanged interfaces.

Definition

Namespace: Common.Core.Classes
Assembly: Common.Core (in Common.Core.dll) Version: 2.1.5
C#
public class ModelDataError : ModelBase, 
	INotifyDataErrorInfo
Inheritance
Object    ModelBase    ModelDataError
Implements
INotifyDataErrorInfo

Remarks

It's base class is ModelBase so that functionality is included.
It can be used as a base class, or as an instance variable.

Example

In a class using it as a base class, the protected constructor will be used:
C#
using System.ComponentModel.DataAnnotations;
using Common.Core.Classes;

public class TestModel : ModelDataError
{
    public bool IsValid => !HasErrors;

    private string? _name;
    [Display( Name = "Full Name" )]
    [Required( ErrorMessage = "Name cannot be empty." )]
    public string Name
    {
        get => ( _name is not null ) ? _name : string.Empty;
        set
        {
            if( value.Equals( _name ) ) return;

            name = value;
            ValidateProperty( value );
            OnPropertyChanged();
        }
    }

    public TestModel()
    { }
}
In a class using it as an instance variable, the public constructor must be used.
The INotifyDataErrorInfo interface must be implemented and the event handler needs to subscribe to the core error version so that the ErrorsChanged event is seen as being used:
C#
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Common.Core.Classes;

public class TestModel : INotifyDataErrorInfo
{
    public bool IsValid => !_validator.HasErrors;

    private string? _name;
    [Display( Name = "Full Name" )]
    [Required( ErrorMessage = "Name cannot be empty." )]
    public string Name
    {
        get => ( _name is not null ) ? _name : string.Empty;
        set
        {
            if( value.Equals( _name ) ) return;
            name = value;
            _validator.ValidateProperty( value );
            OnPropertyChanged();
        }
    }

    private readonly ModelDataError _validator;

    public TestModel()
    {
        // Do this before anything else
        _validator = new ModelDataError( this );
        _validator.ErrorsChanged += Core_ErrorsChanged;
    }

    public bool HasErrors => _validator.HasErrors;

    public IEnumerable GetErrors( string? propertyName )
    {
        return _validator.GetErrors( propertyName );
    }

    public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;

    private void Core_ErrorsChanged( object? sender, DataErrorsChangedEventArgs e )
    {
        ErrorsChanged?.Invoke( this, e );
    }
}

Constructors

ModelDataErrorInitializes a new instance of the ModelDataError class.
ModelDataError(Object)Initializes a new instance of the ModelDataError class.

Properties

HasErrorsGets a value that indicates whether the entity has validation errors.

Methods

ClearErrorsClears the validation errors for a specified property or for the entire entity.
GetErrorsGets the validation errors for a specified property or for the entire entity.
ValidateAllPropertiesValidates all the properties in the current instance.
ValidatePropertyValidates a property with a specified name and a given input value.

Events

ErrorsChangedOccurs when the validation errors have changed for a property or for the entire entity.

See Also