locked
WPF Data Binding Validation Rules RRS feed

  • Question

  • 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.

    • Edited by boogienight Wednesday, January 16, 2013 7:43 PM
    Wednesday, January 16, 2013 7:42 PM

Answers

  • I just noticed that you said something about clicking a button to change the text in the textbox.  I think Validate() only runs when the control being validated loses focus.  I ran a test on this by using your code and changing the text either manually in the textbox or in a button click event.  Because the validation is set in the XAML, I think anything that occurs in the code-behind willl not trigger it.  In order to make sure your code is truly robust, you would need to include validation rules through the XAML (useful for users) and then some additional checks in the code. 

    Hope that helps!  Good luck on the exam, too!


    Christine A. Piffat

    • Marked as answer by boogienight Friday, January 18, 2013 6:13 PM
    Thursday, January 17, 2013 4:32 PM

All replies

  • Because Prop does not contain a setter, it is read-only.  As a result, there is never a time when new data can be entered or validated.  Does it work if you add the setter in your Testor class?

    Christine A. Piffat

    Wednesday, January 16, 2013 8:13 PM
  • Still nothing. I added a setter and a button which sets Prop to another value. Even then Validate() doesn't execute. 

    Any other ideas?

    Wednesday, January 16, 2013 11:16 PM
  • I just noticed that you said something about clicking a button to change the text in the textbox.  I think Validate() only runs when the control being validated loses focus.  I ran a test on this by using your code and changing the text either manually in the textbox or in a button click event.  Because the validation is set in the XAML, I think anything that occurs in the code-behind willl not trigger it.  In order to make sure your code is truly robust, you would need to include validation rules through the XAML (useful for users) and then some additional checks in the code. 

    Hope that helps!  Good luck on the exam, too!


    Christine A. Piffat

    • Marked as answer by boogienight Friday, January 18, 2013 6:13 PM
    Thursday, January 17, 2013 4:32 PM
  • if you want to fire the validate method for label, you need to set:

    <local:StringValidator ValidatesOnTargetUpdated="True" />


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 18, 2013 6:48 AM
  • Thanks everyone!
    Friday, January 18, 2013 6:14 PM