zoukankan      html  css  js  c++  java
  • WPF---数据绑定之ValidationRule数据校验综合Demo(七)

     一、概述

    我们利用ValidationRule以及ErrorTemplate来制作一个简单的表单验证。

    二、Demo

    核心思想:我们在ValidationRule中的Validate函数中进行验证,然后将验证结果存放至一个预先定义好的全局资源中,这样其他控件就可以根据验证结果来进行相应的处理,代码参见以下:

      1 using System.ComponentModel;
      2 using System.Globalization;
      3 using System.Text.RegularExpressions;
      4 using System.Windows;
      5 using System.Windows.Controls;
      6 
      7 namespace BindingDemo5ValidationRuleDemo
      8 {
      9     /// <summary>
     10     /// Interaction logic for MainWindow.xaml
     11     /// </summary>
     12     public partial class MainWindow : Window
     13     {
     14         private string nickName;
     15         public string NickName
     16         {
     17             get { return nickName; }
     18             set { nickName = value; }
     19         }
     20 
     21         private string phoneNumber;
     22         public string PhoneNumber
     23         {
     24             get { return phoneNumber; }
     25             set { phoneNumber = value; }
     26         }
     27 
     28         private string password;
     29         public string Password
     30         {
     31             get { return password; }
     32             set { password = value; }
     33         }
     34 
     35         private string confirmPassword;
     36         public string ConfirmPassword
     37         {
     38             get { return confirmPassword; }
     39             set { confirmPassword = value; }
     40         }
     41 
     42         public MainWindow()
     43         {
     44             InitializeComponent();
     45             this.DataContext = this;
     46         }
     47 
     48         private void Button_Click(object sender, RoutedEventArgs e)
     49         {
     50             MessageBox.Show(NickName);
     51         }
     52     }
     53 
     54     public class ValidationOutput : INotifyPropertyChanged
     55     {
     56         public event PropertyChangedEventHandler PropertyChanged;
     57         public void OnNotifyPropertyChanged(string propertyName)
     58         {
     59             if (PropertyChanged != null)
     60             {
     61                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     62             }
     63         }
     64 
     65         private bool isAllValid = true;//Indicate if all elements is valid
     66 
     67         public bool IsAllValid
     68         {
     69             get { return isAllValid; }
     70             set { isAllValid = value;
     71                 OnNotifyPropertyChanged("IsAllValid");
     72             }
     73         }
     74 
     75 
     76         private string errorMsg4NickName;
     77         public string ErrorMsg4NickName
     78         {
     79             get { return errorMsg4NickName; }
     80             set
     81             {
     82                 errorMsg4NickName = value;
     83                 OnNotifyPropertyChanged("ErrorMsg4NickName");
     84             }
     85         }
     86 
     87         private bool isShow4NickName;
     88         public bool IsShow4NickName
     89         {
     90             get { return isShow4NickName; }
     91             set
     92             {
     93                 isShow4NickName = value;
     94                 OnNotifyPropertyChanged("IsShow4NickName");
     95             }
     96         }
     97 
     98 
     99         private string errorMsg4PhoneNumber;
    100         public string ErrorMsg4PhoneNumber
    101         {
    102             get { return errorMsg4PhoneNumber; }
    103             set
    104             {
    105                 errorMsg4PhoneNumber = value;
    106                 OnNotifyPropertyChanged("ErrorMsg4PhoneNumber");
    107             }
    108         }
    109 
    110         private bool isShow4PhoneNumber;
    111         public bool IsShow4PhoneNumber
    112         {
    113             get { return isShow4PhoneNumber; }
    114             set
    115             {
    116                 isShow4PhoneNumber = value;
    117                 OnNotifyPropertyChanged("IsShow4PhoneNumber");
    118             }
    119         }
    120 
    121         private bool isPhoneNumberValid;
    122         public bool IsPhoneNumberValid
    123         {
    124             get { return isPhoneNumberValid; }
    125             set
    126             {
    127                 isPhoneNumberValid = value;
    128                 OnNotifyPropertyChanged("IsPhoneNumberValid");
    129             }
    130         }
    131 
    132         private bool isNickNameValid;
    133         public bool IsNickNameValid
    134         {
    135             get { return isNickNameValid; }
    136             set
    137             {
    138                 isNickNameValid = value;
    139                 OnNotifyPropertyChanged("IsNickNameValid");
    140             }
    141         }
    142 
    143 
    144 
    145         private string errorMsg4Password;
    146         public string ErrorMsg4Password
    147         {
    148             get { return errorMsg4Password; }
    149             set
    150             {
    151                 errorMsg4Password = value;
    152                 OnNotifyPropertyChanged("ErrorMsg4Password");
    153             }
    154         }
    155 
    156         private bool isPasswordValid;
    157         public bool IsPasswordValid
    158         {
    159             get { return isPasswordValid; }
    160             set
    161             {
    162                 isPasswordValid = value;
    163                 OnNotifyPropertyChanged("IsPasswordValid");
    164             }
    165         }
    166 
    167         private string password;
    168         public string Password
    169         {
    170             get { return password; }
    171             set
    172             {
    173                 password = value;
    174                 OnNotifyPropertyChanged("Password");
    175             }
    176         }
    177 
    178 
    179         private string errorMsg4ConfirmPassword;
    180         public string ErrorMsg4ConfirmPassword
    181         {
    182             get { return errorMsg4ConfirmPassword; }
    183             set
    184             {
    185                 errorMsg4ConfirmPassword = value;
    186                 OnNotifyPropertyChanged("ErrorMsg4ConfirmPassword");
    187             }
    188         }
    189 
    190         private bool isConfirmPasswordValid;
    191         public bool IsConfirmPasswordValid
    192         {
    193             get { return isConfirmPasswordValid; }
    194             set
    195             {
    196                 isConfirmPasswordValid = value;
    197                 OnNotifyPropertyChanged("IsConfirmPasswordValid");
    198             }
    199         }
    200 
    201 
    202     }
    203 
    204     public class RegisterValidationRule : ValidationRule
    205     {
    206         private ValidationOutput validationOutput = null;
    207         public ValidationOutput ValidationOutput
    208         {
    209             get { return validationOutput; }
    210             set { validationOutput = value; }
    211         }
    212         private string validateType;
    213 
    214         public string ValidateType
    215         {
    216             get { return validateType; }
    217             set { validateType = value; }
    218         }
    219 
    220         public bool IsLegalPhoneNumber(string phoneNumber)
    221         {
    222             return Regex.IsMatch(phoneNumber, @"^1[3578]d{9}$");
    223         }
    224         public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    225         {
    226             if (ValidateType == "NickName")
    227             {
    228                 string s = (string)value;
    229                 if (s.Length < 6)
    230                 {
    231                     ValidationOutput.IsNickNameValid = false;
    232                     ValidationOutput.IsAllValid = false;
    233                     ValidationOutput.ErrorMsg4NickName = "Length should be longger than 6 characters";
    234                     return new ValidationResult(false, "Length should be longger than 6 characters");
    235                 }
    236                 else
    237                 {
    238                     ValidationOutput.IsNickNameValid = true;
    239                     ValidationOutput.IsAllValid = true;
    240                     return new ValidationResult(true, null);
    241                 }
    242            
    243             }
    244             else if(ValidateType == "PhoneNumber")
    245             {
    246                 string s = (string)value;
    247                 if (!IsLegalPhoneNumber(s))
    248                 {
    249                     //ValidationOutput.IsShow4PhoneNumber = true;
    250                     ValidationOutput.IsPhoneNumberValid = false;
    251                     ValidationOutput.IsAllValid = false;
    252                     ValidationOutput.ErrorMsg4PhoneNumber = "Phone number format is not correct";
    253                     return new ValidationResult(false, "Phone number format is not correct");
    254                 }
    255                 else
    256                 {
    257                     ValidationOutput.IsShow4PhoneNumber = false;
    258                     ValidationOutput.IsPhoneNumberValid = true;
    259                     ValidationOutput.IsAllValid = true;
    260                     return new ValidationResult(true, null);
    261                 }
    262             }
    263             else if (ValidateType == "Password")
    264             {
    265                 string myPassword = (string)value;
    266                 ValidationOutput.Password = myPassword;//Store the password in a global resource, used for validating the confirm password
    267                 if (myPassword.Length < 8)
    268                 {
    269                     ValidationOutput.IsPasswordValid = false;
    270                     ValidationOutput.IsAllValid = false;
    271                     ValidationOutput.ErrorMsg4Password = "Password length should be longger than 8 characters";
    272                     return new ValidationResult(false, "Password length should be longger than 8 characters");
    273                 }
    274                 else
    275                 {
    276                     ValidationOutput.IsPasswordValid = true;
    277                     ValidationOutput.IsAllValid = true;
    278                     return new ValidationResult(true, null);
    279                 }
    280             }
    281             else if (ValidateType == "ConfirmPassword")
    282             {
    283                 string myConfirmPassword = (string)value;
    284                 string myPassword = ValidationOutput.Password;
    285                 if (myPassword != myConfirmPassword)
    286                 {
    287                     ValidationOutput.IsConfirmPasswordValid = false;
    288                     ValidationOutput.IsAllValid = false;
    289                     ValidationOutput.ErrorMsg4ConfirmPassword = "Password are not the same";
    290                     return new ValidationResult(false, "Password are not the same");
    291                 }
    292                 else
    293                 {
    294                     ValidationOutput.IsConfirmPasswordValid = true;
    295                     ValidationOutput.IsAllValid = true;
    296                     return new ValidationResult(true, null);
    297                 }
    298             }
    299             return new ValidationResult(true, null);
    300 
    301         }
    302 
    303     }
    304 }
    View Code
      1 <Window x:Class="BindingDemo5ValidationRuleDemo.MainWindow"
      2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      6         xmlns:local="clr-namespace:BindingDemo5ValidationRuleDemo"
      7         mc:Ignorable="d"
      8         Title="RegisterWindow" Height="369.738" Width="479.098">
      9     <Window.Resources>
     10         <BooleanToVisibilityConverter x:Key="Bool2Visibility"/>
     11         <local:ValidationOutput x:Key="validateOutput" IsShow4NickName="False"></local:ValidationOutput>
     12         <ControlTemplate x:Key="validationTemplate">
     13             <DockPanel>
     14                 <AdornedElementPlaceholder Name="adorner"/>
     15                 <Grid Margin="20 0">
     16                     <Grid.ColumnDefinitions>
     17                         <ColumnDefinition Width="30"/>
     18                         <ColumnDefinition/>
     19                     </Grid.ColumnDefinitions>
     20                     <Ellipse Width="20" Height="20" Name="elps" Fill="Red"></Ellipse>
     21                     <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20" Foreground="White" Name="tbx"    Text="!"                         
     22                                ToolTip="{Binding ElementName=adorner,  Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}" 
     23                     >
     24                     </TextBlock>
     25                 </Grid>
     26 
     27             </DockPanel>
     28         </ControlTemplate>
     29         <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
     30             <Style.Triggers>
     31                 <Trigger Property="Validation.HasError" Value="true">
     32                     <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}"/>
     33                     <Setter Property="BorderBrush" Value="Red"></Setter>
     34                     <Setter Property="BorderThickness" Value="2"></Setter>
     35                     <Setter Property="Validation.ErrorTemplate" Value="{StaticResource validationTemplate}" />
     36                 </Trigger>
     37             </Style.Triggers>
     38         </Style>
     39     </Window.Resources>
     40 
     41     <Border BorderThickness="1" BorderBrush="#FF1D3061" Margin="20">
     42         <Grid  >
     43             <Grid.RowDefinitions>
     44                 <RowDefinition Height="auto"/>
     45                 <RowDefinition Height="auto"/>
     46                 <RowDefinition Height="auto"/>
     47                 <RowDefinition Height="auto"/>
     48                 <RowDefinition Height="auto" MinHeight="79"/>
     49                 <RowDefinition/>
     50             </Grid.RowDefinitions>
     51             <Grid.ColumnDefinitions>
     52                 <ColumnDefinition Width="141"/>
     53                 <ColumnDefinition Width="*"/>
     54                 <ColumnDefinition Width="100"/>
     55             </Grid.ColumnDefinitions>
     56             <TextBlock Text="昵  称:" FontSize="20" VerticalAlignment="Center" Foreground="#FF416293" Margin="10,14" Height="28"></TextBlock>
     57             <TextBlock Text="电  话:" FontSize="20" VerticalAlignment="Center" Foreground="#FF416293" Margin="10,14" Grid.Row="1" Height="28"></TextBlock>
     58             <TextBlock Text="密  码:" FontSize="20" VerticalAlignment="Center" Foreground="#FF416293" Margin="10,14" Grid.Row="2" Height="27"></TextBlock>
     59             <TextBlock Text="密  码 确 认:" FontSize="20" VerticalAlignment="Center" Foreground="#FF416293" Margin="10,15" Grid.Row="3" Height="26"></TextBlock>
     60 
     61             <TextBox FontSize="20" VerticalAlignment="Center"  Margin="10,12" Grid.Column="1"  Style="{StaticResource textBoxInError}" Height="32" >
     62                 <TextBox.Text>
     63                     <Binding Path="NickName" UpdateSourceTrigger="PropertyChanged">
     64                         <Binding.ValidationRules>
     65                             <local:RegisterValidationRule ValidationOutput="{StaticResource validateOutput}" ValidateType="NickName"/>
     66                         </Binding.ValidationRules>
     67                     </Binding>
     68                 </TextBox.Text>
     69             </TextBox>
     70             <TextBox FontSize="20" VerticalAlignment="Center"  Margin="10,12" Grid.Column="1" Grid.Row="1" Name="tbxPhone" Style="{StaticResource textBoxInError}" Height="32"
     71             >
     72                 <TextBox.Text>
     73                     <Binding Path="PhoneNumber" UpdateSourceTrigger="PropertyChanged">
     74                         <Binding.ValidationRules>
     75                             <local:RegisterValidationRule ValidationOutput="{StaticResource validateOutput}" ValidateType="PhoneNumber"/>
     76                         </Binding.ValidationRules>
     77                     </Binding>
     78                 </TextBox.Text>
     79 
     80             </TextBox>
     81             <TextBox FontSize="20" VerticalAlignment="Center"  Margin="10,11" Grid.Column="1" Grid.Row="2"  Height="33" Style="{StaticResource textBoxInError}">
     82                 <TextBox.Text>
     83                     <Binding Path="Password" UpdateSourceTrigger="PropertyChanged">
     84                         <Binding.ValidationRules>
     85                             <local:RegisterValidationRule ValidationOutput="{StaticResource validateOutput}" ValidateType="Password"/>
     86                         </Binding.ValidationRules>
     87                     </Binding>
     88                 </TextBox.Text>
     89             </TextBox>
     90             <TextBox FontSize="20" VerticalAlignment="Center"  Margin="10,11" Grid.Column="1" Grid.Row="3"  Height="34" Style="{StaticResource textBoxInError}">
     91                 <TextBox.Text>
     92                     <Binding Path="ConfirmPassword" UpdateSourceTrigger="PropertyChanged">
     93                         <Binding.ValidationRules>
     94                             <local:RegisterValidationRule ValidationOutput="{StaticResource validateOutput}" ValidateType="ConfirmPassword"/>
     95                         </Binding.ValidationRules>
     96                     </Binding>
     97                 </TextBox.Text>
     98             </TextBox>
     99             <Button Grid.Row="4" Grid.Column="2" Margin="8,18,8,14" Content="注册" Background="#FF2C6CA2" Foreground="White" Click="Button_Click"
    100                     IsEnabled="{Binding Path=IsAllValid, Source={StaticResource validateOutput}}"></Button>
    101 
    102             <Grid Grid.Column="2" Margin="10,10" Visibility="{Binding Path=IsNickNameValid, Source={StaticResource validateOutput}, Converter={StaticResource Bool2Visibility}}">
    103                 <Grid.ColumnDefinitions>
    104                     <ColumnDefinition Width="30"/>
    105                     <ColumnDefinition/>
    106                 </Grid.ColumnDefinitions>
    107                 <Ellipse Width="20" Height="20" Fill="Green"></Ellipse>
    108                 <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14" Foreground="White">√</TextBlock>
    109                 <!--<TextBlock VerticalAlignment="Center"  FontSize="20" Foreground="Red" Grid.Column="1" 
    110                            Text="{Binding Path=ErrorMsg4NickName, Source={StaticResource validateOutput}}"
    111                       >
    112 
    113                 </TextBlock>-->
    114             </Grid>
    115 
    116             <Grid Grid.Column="2" Grid.Row="1" Margin="10,10" Visibility="{Binding Path=IsPhoneNumberValid, Source={StaticResource validateOutput}, Converter={StaticResource Bool2Visibility}}">
    117                 <Grid.ColumnDefinitions>
    118                     <ColumnDefinition Width="30"/>
    119                     <ColumnDefinition/>
    120                 </Grid.ColumnDefinitions>
    121                 <Ellipse Width="20" Height="20" Fill="Green"></Ellipse>
    122                 <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14" Foreground="White">√</TextBlock>
    123             </Grid>
    124 
    125             <Grid Grid.Column="2" Grid.Row="2" Margin="10,10" Visibility="{Binding Path=IsPasswordValid, Source={StaticResource validateOutput}, Converter={StaticResource Bool2Visibility}}">
    126                 <Grid.ColumnDefinitions>
    127                     <ColumnDefinition Width="30"/>
    128                     <ColumnDefinition/>
    129                 </Grid.ColumnDefinitions>
    130                 <Ellipse Width="20" Height="20" Fill="Green"></Ellipse>
    131                 <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14" Foreground="White">√</TextBlock>
    132             </Grid>
    133 
    134             <Grid Grid.Column="2" Grid.Row="3" Margin="10,10" Visibility="{Binding Path=IsConfirmPasswordValid, Source={StaticResource validateOutput}, Converter={StaticResource Bool2Visibility}}">
    135                 <Grid.ColumnDefinitions>
    136                     <ColumnDefinition Width="30"/>
    137                     <ColumnDefinition/>
    138                 </Grid.ColumnDefinitions>
    139                 <Ellipse Width="20" Height="20" Fill="Green"></Ellipse>
    140                 <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14" Foreground="White">√</TextBlock>
    141             </Grid>
    142         </Grid>
    143     </Border>
    144 </Window>
    View Code

    运行结果以下:

  • 相关阅读:
    【转】Linux 查看CPU信息、机器型号等硬件信息
    荐书
    软件架构的5种视图
    MariaDB 10.4.12 Stable Row size too large (> 8126). Changing some columns to TEXT or BLOB may help. 亲测有效
    配置MariaDB允许远程连接的方法
    MariaDB 10.4.12 Stable 绿色版下载安装
    最完整的Markdown基础教程
    Java开发环境配置 JDK开发环境配置
    centos7 断电导致 generating /run/initramfs/rdsosreport.txt 问题
    高性能分布式锁-redisson
  • 原文地址:https://www.cnblogs.com/3xiaolonglong/p/9781015.html
Copyright © 2011-2022 走看看