Specify Validation Rules for a Binding : Validation « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » Validation 
24.170.3.Specify Validation Rules for a Binding
<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 >= && 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.