Specify Validation Rules for a Binding : ValidationRule « Windows Presentation Foundation « VB.Net Tutorial






<Window 
    x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1"
    Title="WPF" Height="100" Width="280">
    <Window.Resources>
        <Style 
            x:Key="textBoxInErrorStyle" 
            TargetType="{x:Type TextBox}" >
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static 
                                                  RelativeSource.Self}, 
                                Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel DockPanel.Dock="Right">
                            <AdornedElementPlaceholder/>
                            <Image Source="Error.png" Width="16" Height="16"
                               ToolTip="{Binding Path=AdornedElement.ToolTip, RelativeSource={RelativeSource 
                                            Mode=FindAncestor,
                                            AncestorType={x:Type Adorner}}}"/>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>
        <Slider Name="slider" Margin="4" Interval="1" TickFrequency="1" IsSnapToTickEnabled="True" Minimum="0" Maximum="100"/>

        <StackPanel Orientation="Horizontal" >
            <TextBox Style="{StaticResource textBoxInErrorStyle}" HorizontalAlignment="Center" >
              <TextBox.Text>
                    <Binding ElementName="slider" Path="Value" Mode="TwoWay"
                        UpdateSourceTrigger="PropertyChanged" >
                       <Binding.ValidationRules>
                            <WpfApplication1:PercentageRule/>
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox> 
        </StackPanel>
    </StackPanel>
</Window>
//File:Window.xaml.vb
Imports System.Globalization
Imports System.Windows.Controls

Namespace WpfApplication1
  Public Class PercentageRule
    Inherits ValidationRule
    Public Overrides Function Validate(value As Object, cultureInfo As CultureInfo) As ValidationResult
      Dim stringValue As String = TryCast(value, String)
      If Not String.IsNullOrEmpty(stringValue) Then
        Dim doubleValue As Double
        If Double.TryParse(stringValue, doubleValue) Then
          If doubleValue >= 0 AndAlso doubleValue <= 100 Then
            Return New ValidationResult(True, Nothing)
          End If
        End If
      End If
      Return New ValidationResult(False, "Must be a number between 0 and 100")
    End Function
  End Class
End Namespace








16.130.ValidationRule
16.130.1.TextBox with default ErrorTemplateTextBox with default ErrorTemplate
16.130.2.Specify Validation Rules for a BindingSpecify Validation Rules for a Binding