I have control with this check
<MyPicker.SelectedItem> <Binding Path="Person.Value" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True"> <Binding.ValidationRules> <rules:MyValidationRule ValidationType="notnull"/> </Binding.ValidationRules> </Binding> </MyPicker.SelectedItem>
This is the validation class:
class MyValidationRule : ValidationRule { private string _validationType; public string ValidationType { get { return _validationType; } set { _validationType = value; } } public override ValidationResult Validate(object value, CultureInfo cultureInfo) { ValidationResult trueResult = new ValidationResult(true, null); switch (_validationType.ToLower()) { case "notnull": return value == null ? new ValidationResult(false, "EMPTY FIELD") : trueResult; default: return trueResult; } } }
Question: When a property is changed, the Validate () method is called, which is the correct one.
But to call this method at the very beginning when creating MyControl ? I need to prove immediate after initialization if there is a null value in the control (and display a validation error)
validation wpf
PaN1C_Showt1Me
source share