Validation errors prevent calling device properties

I am looking for a simple solution to the following problem:

I am using a simple TextBox control with a Text attribute bound to a property in the code. In addition, I use a validation rule to notify the user of incorrect entries.

                                               ... style display error here ...  

Now, after entering the correct data into the TextBox, the user can click the button to send the data. When a button is clicked, data from the associated UserName property in the code behind is evaluated and sent.

The problem is that the user can enter valid data in the TextBox, and this will be set in the UserName property. If the user then decides to change the text in the TextBox and the data becomes invalid, the installer of the UserName property is not called after a failed validation.

This means that the last valid data remains in the UserName property, while the TextBox displays invalid data with an error indicator. If the user then clicks the button to send data, the last valid data will be sent instead of the current contents of the TextBox.

I know that I can deactivate the button if the data is invalid and in fact I do it, but the method is called in the UserName installer. And if this is not called after a failed check, the button remains on.

So the question is: how do I enable the call to the property settings tool after a failed check?

+3
source share
2 answers

How I handle this in my view model classes:

public class MyViewModel : INotifyPropertyChanged, IDataErrorInfo
{
   private Dictionary<string, string> _Errors = new Dictionary<string, string>();

   public object SomeProperty
   {
      get { return _SomeProperty; }
      set
      {
         if (value != _SomeProperty && !ValidationError("SomeProperty", value))
            _SomeProperty = value;
            OnPropertyChanged("SomeProperty");
         }
      }
   }

   private bool ValidationError(string propertyName, object value)
   {
      // I usually have a Dictionary<string, Func<object, string>> that maps property
      // names to validation functions; the functions return null if the property
      // is valid and an error message if not.  You can embed the validation logic
      // in the property setters, of course, but breaking them out as separate methods
      // eases testing.
      _Errors[propertyName] = _ValidationMethods[propertyName](value);
      OnPropertyChanged("IsValid");
   }

   public bool IsValid
   {
      get { return !(_Errors.Where(x => x.Value != null).Any()));
   }

   public string this[string propertyName]
   {
      get
      {
         return (_Errors.ContainsKey(propertyName))
            ? _Errors[propertyName]
            : null;
      }
   }
}

, , , ( DataErrorValidationRule), , - ( _Errors) IsValid, , . ( , IsValid, , , , , .) internal, NUnit - .

, - , , .

+2

ValidationRule.ValidationStep ValidationStep.UpdatedValue. , . , , . , .NET 3.5 SP1 . . ( " ? ( 1)" ).

+1

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


All Articles