Question

I have a WPF application(.Net 3.5) which uses the IDataErrorInfo on the ViewModel to validate input.

It works great, the usercontrol get the correct UI feedback.

The problem is that the user can still change the selected element, or save this element.

So my question is: How can I know that all my properties are valid? Or at least that all my displayed values are valid. The goal is to bind some IsActive on this result

Was it helpful?

Solution

From your comment on your implementation of IDataErrorInfo change your implementation to this style....

#region IDataErrorInfo Members

public string Error
{
    get { return this[null] }
}

public string this[string columnName]
{
    get
    {
        StringBuilder result = new StringBuilder();
        if (string.IsNullOrEmpty(columnName) || columnName == "FirstName")
        {
            if (string.IsNullOrEmpty(FirstName))
                result.Append("Please enter a First Name\n");
        }
        if (string.IsNullOrEmpty(columnName) || columnName == "LastName")
        {
            if (string.IsNullOrEmpty(LastName))
                result.Append("Please enter a Last Name\n");
        }
       if (string.IsNullOrEmpty(columnName) || columnName == "Age")
        {
            if (Age < = 0 || Age >= 99)
                result.Append("Please enter a valid age\n");
        }
        return (result.Length==0) ? null : result.Remove(result.Length-1,1).ToString();
    }
}

#endregion


public bool IsValid {
   get { return string.IsNullOrEmpty(this.Error); }
}

Then in your property changed event

if (e.PropertyName == "Error") {
   OnPropertyChanged(this,new PropertyChangedEventArgs("IsValid"));
}
if (e.PropertyName != "Error" && e.PropertyName != "IsValid") {
   OnPropertyChanged(this,new PropertyChangedEventArgs("Error"));
}

OTHER TIPS

For now, I added this method on my model.

    public Boolean IsModelValid()
    {
        Boolean isValid = true;
        PropertyInfo[] properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

        foreach (PropertyInfo p in properties)
        {
            if (!p.CanWrite || !p.CanRead)
            {
                continue;
            }
            if (this[p.Name] != null)
            {
                isValid = false;
            }
        }
        return isValid;
    }

I bound the object itself on the PropertyChanged event,

    public MyClassName()
    {
        PropertyChanged += CheckModelValidity;
        CheckModelValidity(null, null);
    }

when it change, I call this method, and if the result is different than my actual public member, I update it:

    private void CheckModelValidity(object sender, PropertyChangedEventArgs e)
    {
        bool isModelValid = IsModelValid();
        if(isModelValid!= IsValid)
        {
            IsValid = isModelValid;
        }
    }

And then I can just bind the IsValid property.

I don't know if there is a better solution?

Thank you Bob Vale for the idea! I noticed that I have several models and the code is very repetitive. I created this base class:

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace Core.Models
{
    public abstract class ValidatableObject : IDataErrorInfo
    {
        public virtual bool IsValid() => this[null] == null;

        [NotMapped]
        public string? Error => this[null];

        [NotMapped]
        protected abstract (string, Func<string?>)[] Checks { get; }

        public virtual string? this[string? name] => validate(name, Checks);

        private static string? validate(string? name, params (string, Func<string?>)[] checks)
        {
            StringBuilder results = new();

            foreach ((string val, Func<string?> check) in checks)
            {
                if (String.IsNullOrEmpty(name) || name == val)
                {
                    string? result = check();
                    if (result != null)
                        results.AppendLine(result);
                }
            }

            return results.Length == 0
                ? null
                : results.ToString(0, results.Length - Environment.NewLine.Length);
        }
    }
}

And here's usage:

    private class Validatable : ValidatableObject
    {
        public string Email { get; set; }
        public string Comment { get; set; }

        protected override (string, Func<string?>)[] Checks => new (string, Func<string?>)[]
        {
            (nameof(Email), () => Validate.Email(Email)),
            (nameof(Comment), () => Validate.LengthOfOptionalString(Comment)),
        };
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top