Specify Validation Rules for a Binding : Validation « Windows Presentation Foundation « C# / CSharp 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.cs
using System.Globalization;
using System.Windows.Controls;

namespace WpfApplication1
{
    public class PercentageRule : ValidationRule
    {
        public override ValidationResult Validate(object value,CultureInfo cultureInfo)
        {
            string stringValue = value as string;
            if(!string.IsNullOrEmpty(stringValue))
            {
                double doubleValue;
                if(double.TryParse(stringValue, out doubleValue))
                {
                    if(doubleValue >= 0 && doubleValue <= 100)
                    {
                        return new ValidationResult(true, null);
                    }
                }
            }
            return new ValidationResult(false, "Must be a number between 0 and 100");
        }
    }
}
WPF Specify Validation Rules For A Binding








24.170.Validation
24.170.1.Implement validation logic on custom objects and then bind to them(Mouse-over to see the validation error message).Implement validation logic on custom objects and then bind to them(Mouse-over to see the validation error message).
24.170.2.IValueConverter and ValidationRuleIValueConverter and ValidationRule
24.170.3.Specify Validation Rules for a BindingSpecify Validation Rules for a Binding