Welcome to WindowsClient.net | My Blog | Sign in | Join

Windows Client Videos

How Do I: Create a Custom Binding Validator

When applying validators to bindings in WPF, there are two that are available by default. Most times you will want to create your own validators. Custom validators are easy to create and easier to use. In this video, Todd Miranda demonstrates how to create custom validators for WPF bindings.

Author: Todd Miranda

Comments

devador said:

Thank you for the interesting video to create a validator! it was very helpfully. http://www.iseo-gmbh.de

# September 8, 2009 2:58 PM

RAGolko said:

Can you show how to display the nice error message you put in the Age Validator?  I can't see how to do that.

# November 16, 2009 6:41 AM

Sachsenwald said:

Nice infos.Great work

# January 29, 2010 2:58 PM

Partnerringe said:

nice vid, and a good designed page! best wishes

# November 24, 2010 4:30 AM

blandau said:

To answer RAGolko:

First, need to add a Window resource for the validation error template.

<Window.Resources>

   <ControlTemplate x:Key="validationTemplate">

       <DockPanel>

           <TextBlock DockPanel.Dock="Right" Text="{Binding ElementName=adorn, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>

           <AdornedElementPlaceholder Name="adorn"/>

       </DockPanel>

   </ControlTemplate>

</Window.Resources>

Next, add the Validation.ErrorTemplate property to TextBox control:

<TextBox Grid.Column="1" Grid.Row="1" Width="100" HorizontalAlignment="Left"

        Validation.ErrorTemplate="{StaticResource validationTemplate}">

   <TextBox.Text>

       <Binding Path="Age">

           <Binding.ValidationRules>

               <local:AgeValidation />

           </Binding.ValidationRules>

       </Binding>

   </TextBox.Text>

</TextBox>

Note: I specified a width of the TextBox so I could see the error with out having to adjust the window to far.

# May 31, 2011 6:47 PM