Comment intercepter la validation DataAnnotations dans MVVM

Comment puis-je attraper la validation de DataAnnotations? je recherche ici mais je ne comprenais pas comment ça marche

donc j’espère que certains d’entre vous peuvent éclairer mon

voici mon code de test actuel:

Modèle

public class Person // Represents person data. { ///  /// Gets or sets the person's first name. ///  ///  /// Empty ssortingng or null are not allowed. /// Allow minimum of 2 and up to 40 uppercase and lowercase. ///  [Required] [RegularExpression(@"^[a-zA-Z''-'\s]{2,40}$")] public ssortingng FirstName{ get; set;} ///  /// Gets or sets the person's last name. ///  ///  /// Empty ssortingng or null are not allowed. ///  [Required] public ssortingng LastName { get; set;} public int Age{ get; set;} } 

Vue

             

Dois-je implémenter autre chose à Person? J’ai trouvé ici le code suivant, mais comme je l’ai dit auparavant, je ne comprenais pas comment cela fonctionnait -.-

 public static T GetAtsortingbuteFrom(this object instance, ssortingng propertyName) where T : Atsortingbute { var attrType = typeof(T); var property = instance.GetType().GetProperty(propertyName); return (T)property .GetCustomAtsortingbutes(attrType, false).First(); } 

la solution

 public class Person : IDataErrorInfo // Represents person data. { ///  /// Gets or sets the person's first name. ///  ///  /// Empty ssortingng or null are not allowed. /// Allow minimum of 2 and up to 40 uppercase and lowercase. ///  [Required] [RegularExpression(@"^[a-zA-Z''-'\s]{2,40}$")] public ssortingng FirstName{ get; set;} ///  /// Gets or sets the person's last name. ///  ///  /// Empty ssortingng or null are not allowed. ///  [Required] public ssortingng LastName { get; set;} public int Age{ get; set;} public ssortingng Error // Part of the IDataErrorInfo Interface { get { throw new NotImplementedException(); } } ssortingng IDataErrorInfo.this[ssortingng propertyName] // Part of the IDataErrorInfo Interface { get { return OnValidate(propertyName); } } ///  /// Validates current instance properties using Data Annotations. ///  ///  ///  protected virtual ssortingng OnValidate(ssortingng propertyName) { if (ssortingng.IsNullOrEmpty(propertyName)) throw new ArgumentException("Invalid property name", propertyName); ssortingng error = ssortingng.Empty; var value = this.GetType().GetProperty(propertyName).GetValue(this, null); var results = new List(1); var context = new ValidationContext(this, null, null) { MemberName = propertyName }; var result = Validator.TryValidateProperty(value, context, results); if (!result) { var validationResult = results.First(); error = validationResult.ErrorMessage; } return error; } } 

merci à Rachel pour cet indice et à ce lien qui était très éclairé