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

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Development
8.Event
9.File Directory
10.Generics
11.GUI
12.Language Basics
13.LINQ
14.Network Remote
15.Security
16.Thread
17.Windows Presentation Foundation
18.Windows System
19.XML
20.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Windows Presentation Foundation » ValidationRuleScreenshots 
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.vb
Imports System.Globalization
Imports System.Windows.Controls

Namespace WpfApplication1
  Public Class PercentageRule
    Inherits ValidationRule
    Public Overrides Function Validate(value As Object, cultureInfo As CultureInfoAs ValidationResult
      Dim stringValue As String = TryCast(value, String)
      If Not String.IsNullOrEmpty(stringValueThen
        Dim doubleValue As Double
        If Double.TryParse(stringValue, doubleValueThen
          If doubleValue >= 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

   
    
    
    
    
  
Related examples in the same category
1.TextBox with default ErrorTemplateTextBox with default ErrorTemplate
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.