<!-- 操作系统支持 --> <operatingsystemconditions> <!-- Windows XP SP2+ --> <operatingsystemconditionMajorVersion="5"MinorVersion="1" PlatformId="2"CSDVersion="" Bits="1"ProductType="1" ServicePackMajorMin="2"/> <!-- Windows Server 2003 --> <operatingsystemconditionMajorVersion="5"MinorVersion="2" PlatformId="2"ProductType="2|3"/> <!-- Windows Server 2003 x64 --> <operatingsystemconditionMajorVersion="5"MinorVersion="2" PlatformId="2"Bits="2"ProductType="1"/> <!-- Windows Vista --> <operatingsystemconditionMajorVersion="6"MinorVersion="0" PlatformId="2"/> <!-- Windows Server 2008 --> <operatingsystemconditionMajorVersion="6"MinorVersion="0" PlatformId="2"ProductType="2|3"/> <!-- Windows 7 / Server 2008 R2 --> <operatingsystemconditionMajorVersion="6"MinorVersion="0" PlatformId="2"ProductType="1"/> </operatingsystemconditions>
publicclassPersonValidateInSetter : ObservableObject { privatestring name; privateint age; publicstring Name { get { returnthis.name; } set { if (string.IsNullOrWhiteSpace(value)) { thrownew ArgumentException("Name cannot be empty!"); } if (value.Length < 4) { thrownew ArgumentException("Name must have more than 4 char!"); } this.name = value; this.OnPropertyChanged(() => this.Name); } } publicint Age { get { returnthis.age; } set { if (value < 18) { thrownew ArgumentException("You must be an adult!"); } this.age = value; this.OnPropertyChanged(() => this.Age); } } }
publicclassPersonDerivedFromIDataErrorInfo : ObservableObject, IDataErrorInfo { privatestring name; privateint age; publicstring Name { get { returnthis.name; } set { this.name = value; this.OnPropertyChanged(() => this.Name); } } publicint Age { get { returnthis.age; } set { this.age = value; this.OnPropertyChanged(() => this.Age); } } // never called by WPF publicstring Error { get { returnnull; } } publicstringthis\[string propertyName\] { get { switch (propertyName) { case"Name": if (string.IsNullOrWhiteSpace(this.Name)) { return"Name cannot be empty!"; } if (this.Name.Length < 4) { return"Name must have more than 4 char!"; } break; case"Age": if (this.Age < 18) { return"You must be an adult!"; } break; } returnnull; } } }
publicclassMinAgeValidation : ValidationRule { publicint MinAge { get; set; } publicoverride ValidationResult Validate(objectvalue, CultureInfo cultureInfo) { ValidationResult result \= null; if (value != null) { int age; if (int.TryParse(value.ToString(), out age)) { if (age < this.MinAge) { result \= new ValidationResult(false, "Age must large than " + this.MinAge.ToString(CultureInfo.InvariantCulture)); } } else { result \= new ValidationResult(false, "Age must be a number!"); } } else { result \= new ValidationResult(false, "Age must not be null!"); } returnnew ValidationResult(true, null); } }