3

Are there any recommendations about performing asynchronous MVVM-ish validations in WPF? Have read about INotifyDataErrorInfo, but unfortunately is only available to Silverlight.

Thanks.

1 Answer 1

1

IDataErrorInfo is the data validation mechanism for WPF. Don't you just love Microsoft's consistency? ;)

Implement IDataErrorInfo on your ViewModel like this:

public class MyViewModel : IDataErrorInfo
{
       public string Error
        {
            get { 
              return  GetErrorStringForThisViewModelInGeneral();
            }
        }

        public string this[string columnName]
        {
            get
            {
                string result = null;

                switch (columnName)
                {
                    case "Quantity":
                        if (Quantity <= 0)
                            result = "Quantity must be greater than 1.";
                    break;
                }
                return result;
            }

}

Inside of the property (aka this[]) validation, you could use the validator in the EnterpriseLibrary, a custom validator using Attributes, or anything you like. I am just showing a basic implementation to get you started.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, I was referring to asynchronous validations - i.e Quantity is validated through a web service.
To make the validation asynchronous, you should spin a thread to do the validation (to avoid hanging the application), then when the result returns do a NotifyPropertyChanged on the property being validated (e.g. Quantity). When the Notify happens, WPF will revalidate the field, and you should be able to set a condition so it doesn't revalidate remotely (as the value hasn't changed since the last validation).
That's what I thought - shame it requires such a boilerplate code :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.