WPF 验证控件

WPF 验证控件

<Window x:Class= "WpfApplication2.ValidationTest"
     xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:local= "clr-namespace:WpfApplication2"
     Title= "ValidationTest"  Height= "300"  Width= "800" >
     <Window.Resources>
         <Style TargetType= "{x:Type TextBox}" >
             <Setter Property= "Validation.ErrorTemplate" >
                 <Setter.Value>
                     <ControlTemplate>
                         <DockPanel LastChildFill= "True" >
                             <TextBlock DockPanel.Dock= "Right"
                                  Foreground= "Red"
                                  Margin= "5"
                                  FontSize= "12pt"
                                  Text="{Binding ElementName=MyAdorner,
                                  Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                             </TextBlock>
 
                             <Border BorderBrush= "Green"  BorderThickness= "3" >
                                 <AdornedElementPlaceholder Name= "MyAdorner"  />
                             </Border>
 
                         </DockPanel>
                     </ControlTemplate>
                 </Setter.Value>
             </Setter>
             <Style.Triggers>
                 <Trigger Property= "Validation.HasError"  Value= "true" >
                     <Setter Property= "ToolTip"
                        Value="{Binding RelativeSource={RelativeSource Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
                 </Trigger>
             </Style.Triggers>
         </Style>
 
     </Window.Resources>
 
 
     <Grid>
         <TextBox Width= "200"  Height= "50"  Margin= "5"  VerticalAlignment= "Top" >
             <TextBox.Text>
                 <Binding Path= "Name"  UpdateSourceTrigger= "PropertyChanged" >
                     <Binding.ValidationRules>
                         <ExceptionValidationRule />
                     </Binding.ValidationRules>
                 </Binding>
             </TextBox.Text>
 
         </TextBox>
 
         <TextBox Width= "200"  Margin= "5"  Height= "50"  VerticalAlignment= "Center" >
             <TextBox.Text>
                 <Binding Path= "Address"  UpdateSourceTrigger= "PropertyChanged" >
                     <Binding.ValidationRules>
                         <local:StringRangeValidationRule MinimumLength= "1"  MaximumLength= "30"
                          ErrorMessage= "Address is required and must be less than 30 letters."  />
                     </Binding.ValidationRules>
                 </Binding>
             </TextBox.Text>
         </TextBox>
 
 
         <TextBox Text= "HI"  Height= "50"  Width= "200"  VerticalAlignment= "Bottom" />
 
 
     </Grid>
</Window>

  

后台代码:

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Windows;
using  System.Windows.Controls;
using  System.Windows.Data;
using  System.Windows.Documents;
using  System.Windows.Input;
using  System.Windows.Media;
using  System.Windows.Media.Imaging;
using  System.Windows.Shapes;
 
namespace  WpfApplication2
{
     /// <summary>
     /// Interaction logic for ValidationTest.xaml
     /// </summary>
     public  partial  class  ValidationTest : Window
     {
         public  ValidationTest()
         {
             InitializeComponent();
 
             SimpleCustomer c = new  SimpleCustomer();
             c.Name = "Fred" ;
             c.Address = "1/3 Powell Street" ;
             this .DataContext = c;
 
         }
     }
 
     /// <summary>
     /// 异常验证
     /// </summary>
     public  class  SimpleCustomer
     {
         public  SimpleCustomer()
         { }
 
         private  string  m_strName;
         public  string  Name
         {
             set
             {
                 this .m_strName = value;
                 if  (String.IsNullOrEmpty( this .m_strName))
                 {
                     throw  new  ApplicationException( "Customer name is mandatory." );
                 }
             }
             get
             {
                 return  this .m_strName;
             }
         }
 
         private  string  m_strAddress;
         public  string  Address
         {
             set
             {
                 this .m_strAddress = value;
             }
             get
             {
                 return  this .m_strAddress;
             }
         }
     }
 
     /// <summary>
     /// 自定义验证
     /// </summary>
     public  class  StringRangeValidationRule : ValidationRule
     {
         public  StringRangeValidationRule()
         { }
 
         private  int  m_intMinimumLength = -1;
         public  int  MinimumLength
         {
             set
             {
                 this .m_intMinimumLength = value;
             }
             get
             {
                 return  this .m_intMinimumLength;
             }
         }
 
         private  int  m_intMaximumLength = -1;
         public  int  MaximumLength
         {
             set
             {
                 this .m_intMaximumLength = value;
             }
             get
             {
                 return  this .m_intMaximumLength;
             }
         }
 
         private  string  m_strErrorMessage = string .Empty;
         public  string  ErrorMessage
         {
             set
             {
                 this .m_strErrorMessage = value;
             }
             get
             {
                 return  this .m_strErrorMessage;
             }
         }
 
 
 
         public  override  ValidationResult Validate( object  value, System.Globalization.CultureInfo cultureInfo)
         {
             ValidationResult result = new  ValidationResult( true , null );
             string  inputString =(value ??  string .Empty).ToString();
             if  ( inputString == null )
             {
                 inputString = string .Empty;
             }
 
             if (inputString.Length < this .m_intMinimumLength || inputString.Length > this .m_intMaximumLength)
             {
                 return  new  ValidationResult( false , this .ErrorMessage);
             }
             return  result;
         }
     }
 
 
 
  
 
 
}

  


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2011/12/26/2172466.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值