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; } } }