Infosys Microsoft Alliance and Solutions blog

« Windows 7 | Main | Understanding an ERP implementation – Bridge Analogy »

WPF - Performing Conditional Validation

When we look at conditional validation needs in WPF, the typical option that people talk about is the usage of UpdateSourceTrigger property in binding expression. There are many examples that one can find on setting this to explicit and managing the validation or things like setting this to PropertyChange when validating textbox input without having to focus out of it.

However there can be scenarios where you can't use explicit since you want the normal two way binding behavior, but have a need to validate based on value of some other control. I had a similar scenario the other day where we had a combo box with yes/no options and the validation had to happen only if a value of "yes" was selected in the combo box.

The idea that I tried out was to do a trigger based binding on the textbox and enable the validation accordingly. One can easily look at setting some globaly property or pass a parameter to the validation rule and in the implementation check for the condition. But this woud mean that the validation rule still fired. I wanted to do away with this extra firing when not required.

So in the textbox, I bound it's Tag property to the combo box's Text property as below.

        <TextBox Style="{StaticResource validationStyle}" Name="textBox2" Tag="{Binding ElementName=comboBox1, Path=Text}" />

And then in the style that I had created for the textbox, I created a trigger, which would fire when the textbox's Tag property has a value of "yes". In this trigger, I applied the binding with the validation, else it was a nomal binding (without the validation). The style was defined as below

        <Style x:Key="validationStyle" TargetType="{x:Type TextBox}">

            <Setter Property="Text">

                <Setter.Value>

                    <Binding Path="Salary" Source="{StaticResource getEmployee}" />

                </Setter.Value>

            </Setter>

            <Style.Triggers>

                <Trigger Property="Tag" Value="Yes">

                    <Setter Property="Text">

                        <Setter.Value>

                            <Binding Path="Salary" Source="{StaticResource getEmployee}" UpdateSourceTrigger="PropertyChanged" >

                                <Binding.ValidationRules>

                                    <local:RangeValidationRule MinSal="1000" MaxSal="10000" />

                                </Binding.ValidationRules>

                            </Binding>

                        </Setter.Value>

                    </Setter>

                </Trigger>

            </Style.Triggers>

        </Style>

This got me the behavior I was looking for. The UpdateSourceTrigger was used to overcome the default value of lost focus for textbox, so that validation could fire as the user would be typing. There are two subtle issues though with this implementation

1. The binding expression is set again in the trigger. Ideally it should have been that i query the existing binding expression and add validation rule to it. However that would have meant that I will need to add the reverse trigger also to remove the validation rule.

2. After changing the value in the combo box to yes, the validation won't fire immediately. I had to type something in the textbox for it to work. I could handle the combo box selection changed event handler to address this.

You can welcome to try to add these behaviors and revert. Hope this helps !
 

 

 

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter