Hello everyone,
I'm studying for Windows Application Development exam and there is one thing about data binding validation rules that's bugging me.
I have a class Testor with a property that always returns "abc".
public class Testor { public Testor() { } public string Prop { get { return "abc"; } } }
I add the namespace in XAML, and instantiate an object of Testor:
<Window.Resources><local:Testor x:Key="myTestor"></local:Testor></Window.Resources>
Then I bind its property "Prop" to a Label control - which correctly displays "abc".
<Label Margin="192,113,199,162"><Label.Content><Binding Source="{StaticResource myTestor}" Path="Prop" NotifyOnValidationError="True"><Binding.ValidationRules><local:StringValidator/> </Binding.ValidationRules></Binding></Label.Content></Label>
What I would like is to have validation of the bound "Prop" property. So I derive a class from ValidationRule like this:
public class StringValidator : ValidationRule { public override ValidationResult Validate(object value, CultureInfo cultureInfo) { if (value.ToString() == "") return new ValidationResult(false, "String cannot be empty"); return new ValidationResult(true, null); } }
And then I add it to XAML under Binding.ValidationRules.
Why does Validate() method never run? I'm clearly missing something but I can't figure it out.