Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IDataErrorInfo : How to know if all properties are valid?

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

like image 957
J4N Avatar asked Nov 28 '12 10:11

J4N


1 Answers

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"));
}
like image 155
Bob Vale Avatar answered Nov 08 '22 03:11

Bob Vale