• 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 }
  • 相关阅读:
    cmd 中英文 切换
    comparable和comparator
    ORACLE 毫秒转换为日期 日期转换毫秒
    asp.net弹出对话框
    一、DataBinder.Eval的基本格式
    转载 创业
    常用的js验证数字,电话号码,传真,邮箱,手机号码,邮编,日期
    CodeSmith开发系列资料总结
    SQL函数说明大全
    SQLServer游标的使用
  • 原文地址:https://www.cnblogs.com/luluping/p/2039483.html
Copyright © 2020-2023  润新知