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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Windows Presentation Foundation » ValidationScreenshots 
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");
        }
    }
}

   
    
  
Related examples in the same category
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).
2.IValueConverter and ValidationRuleIValueConverter and ValidationRule
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.