Using DataAnnotations for validation in MVVM

I discovered new SL3 data annotation features and I use them to validate user input.

I have data like this:

            <dataInput:Label  Target="{Binding ElementName=inputName}"/>
            <TextBox 
                x:Name="inputName" 
                Text="{Binding RequestDemoData.Name, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}"/>
            <dataInput:DescriptionViewer {Binding ElementName=inputName}"/>

and my model looks like this:

   [Display(ResourceType = typeof(Resources.Resources), Name = "Name", Description = "NameDescription")]
    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "NameRequired")]
    [RegularExpression(@"^[^0-9]*[a-zA-Z]+[^0-9]*$", ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "NameError")]
    public string Name
    {
        get
        {
            ValidateProperty("Name", _name);
            return _name;
        }
        set
        {
            if (_name != value)
            {
                ValidateProperty("Name", value);
                _name = value;
                OnPropertyChanged("Name");
            }
        }
    }

So far so good. If the user enters incorrect data, I get an error when he / she focuses. The problem is that I have a submit button tied to ICommand, and I cannot figure out how to make the error message appear when the user clicks on it.

The bad way is to add the code and do it GetBindingExpression(foo).UpdateSource(), and that would figure it out. The downside is that it is completely unmanageable, and I hate using code in my opinion.

http://www.thejoyofcode.com/Silverlight_Validation_and_MVVM_Part_II.aspx , , , .

.

+3
2

, . - .

, , Silverlight. JoyOfCode - .

, , .

0

Josh LOB, , . , , , .

0

Source: https://habr.com/ru/post/1723122/


All Articles