zoukankan      html  css  js  c++  java
  • WPFBinding对数据的检验

    设置Binding的ValidationRules属性对Binding进行检验

    1 <StackPanel>
    2        <TextBox x:Name="txtAge" FontSize="30" Foreground="Red"></TextBox>
    3        <TextBlock x:Name="errorSummary" FontSize="30" Foreground="Red"></TextBlock>
    1 </StackPanel>

    后台代码

    01 public partial class MainWindow : Window
    02     {
    03           
    04         public MainWindow()
    05         {
    06             InitializeComponent();
    07             Person p = new Person { Age = 20, Name = "Tom" };
    08             Binding binding = new Binding("Age") { Source = p };
    09   
    10             binding.NotifyOnValidationError = true;
    11   
    12             binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    13             RangeValidationRule rv = new RangeValidationRule();
    14             binding.ValidationRules.Add(rv);
    15             this.txtAge.SetBinding(TextBox.TextProperty, binding);
    16   
    17             this.txtAge.AddHandler(Validation.ErrorEvent, new RoutedEventHandler(this.ValidationError));
    18         }
    19   
    20         void ValidationError(object sender, RoutedEventArgs e)
    21         {
    22             if (Validation.GetErrors(this.txtAge).Count > 0)
    23             {
    24                 this.txtAge.ToolTip = Validation.GetErrors(this.txtAge)[0].ErrorContent.ToString();
    25                 this.errorSummary.Text = Validation.GetErrors(this.txtAge)[0].ErrorContent.ToString();
    1                 // You can do everything here when validation error occurs
    2             }
    3         }
    4           
    5     }
    6 }

    image

    同样,我们在XAML里也可以设置验证

    01 <StackPanel>
    02         <TextBox x:Name="txtAge" FontSize="30" Foreground="Red"   Validation.Error="txtAge_Error">
    03             <Binding NotifyOnValidationError="True" Path="Age" UpdateSourceTrigger="PropertyChanged">
    04                 <Binding.ValidationRules>
    05                     <local:RangeValidationRule></local:RangeValidationRule>
    06                 </Binding.ValidationRules>
    07             </Binding>
    08         </TextBox>
    09         <TextBlock x:Name="errorSummary" FontSize="30" Foreground="Red"></TextBlock>
    10 </StackPanel>

    后台代码:

    01 public partial class MainWindow : Window
    02     {        
    03         public MainWindow()
    04         {
    05             InitializeComponent();
    06             Person p = new Person { Age = 20, Name = "Tom" };
    07             this.DataContext = p;           
    08         }        
    09         private void txtAge_Error(object sender, ValidationErrorEventArgs e)
    10         {
    11             if (Validation.GetErrors(this.txtAge).Count > 0)
    12             {
    13                 this.txtAge.ToolTip = Validation.GetErrors(this.txtAge)[0].ErrorContent.ToString();
    14   
    15                 this.errorSummary.Text = Validation.GetErrors(this.txtAge)[0].ErrorContent.ToString();
    16                 // You can do everything here when validation error occurs
    17             }
    18         }        
    19 }
  • 相关阅读:
    原生JS写轮播图(转自https://www.cnblogs.com/LIUYANZUO/p/5679753.html)
    用JS写成简易计算器(转自https://mp.weixin.qq.com/s/Jxe3V7D0PFLvIFNZPlSyNg)
    Js获取当前日期时间的方法
    三元运算符的应用(转自百度百科)
    <pre> 标签的简要作用(以前未接触过的)
    用JavaScript实现两种功能:1、切换全选/全不选文字;2、根据选中个数更新全选框状态;(实例代码来自view-source:http://www.fgm.cc/learn/lesson2/12.html)
    Windows 网络通讯开发
    函数指针的使用
    cin的使用问题
    markdown简介
  • 原文地址:https://www.cnblogs.com/luluping/p/2039483.html
Copyright © 2011-2022 走看看