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,如需转载请自行联系原作者

目录
相关文章
浅谈WPF之装饰器实现控件锚点
使用过visio的都知道,在绘制流程图时,当选择或鼠标移动到控件时,都会在控件的四周出现锚点,以便于修改大小,移动位置,或连接线等,那此功能是如何实现的呢?在WPF开发中,想要在控件四周实现锚点,可以通过装饰器来实现,今天通过一个简单的小例子,简述如何在WPF开发中,应用装饰器,仅供学习分享使用,如有不足之处,还请指正。
613 1
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库
1702 0
|
C# Windows
WPF技术之RichTextBox控件
WPF RichTextBox是Windows Presentation Foundation (WPF)中提供的一个强大的文本编辑控件,它可以显示富文本格式的文本,支持多种文本处理操作。
1217 0
|
前端开发 C# 容器
浅谈WPF之控件拖拽与拖动
使用过office的visio软件画图的小伙伴都知道,画图软件分为两部分,左侧图形库,存放各种图标,右侧是一个画布,将左侧图形库的图标控件拖拽到右侧画布,就会生成一个新的控件,并且可以自由拖动。那如何在WPF程序中,实现类似的功能呢?今天就以一个简单的小例子,简述如何在WPF中实现控件的拖拽和拖动,仅供学习分享使用,如有不足之处,还请指正。
744 2
|
开发框架 缓存 前端开发
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(11) -- 下拉列表的数据绑定以及自定义系统字典列表控件
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(11) -- 下拉列表的数据绑定以及自定义系统字典列表控件
|
C# 开发者 Windows
一款基于Fluent设计风格、现代化的WPF UI控件库
一款基于Fluent设计风格、现代化的WPF UI控件库
976 1
|
C# Windows
WPF中如何使用HandyCotrol控件库
WPF中如何使用HandyCotrol控件库
2670 1
|
开发框架 前端开发 JavaScript
WPF应用开发之控件动态内容展示
WPF应用开发之控件动态内容展示
|
开发框架 前端开发 JavaScript
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(3)--自定义用户控件
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(3)--自定义用户控件
|
C# 前端开发 UED
WPF数据验证实战:内置控件与自定义规则,带你玩转前端数据验证,让你的应用程序更上一层楼!
【8月更文挑战第31天】在WPF应用开发中,数据验证是确保输入正确性的关键环节。前端验证能及时发现错误,提升用户体验和程序可靠性。本文对比了几种常用的WPF数据验证方法,并通过示例展示了如何使用内置验证控件(如`TextBox`)及自定义验证规则实现有效验证。内置控件结合`Validation`类可快速实现简单验证;自定义规则则提供了更灵活的复杂逻辑支持。希望本文能帮助开发者更好地进行WPF数据验证。
764 0