WPF应用Binding之数据有效性检测

  1. <Grid>  
  2.     <StackPanel>  
  3.         <TextBox x:Name="textBox1" Margin="5"/>  
  4.         <Slider x:Name="slider1" Minimum="-10" Maximum="110" Margin="5"/>  
  5.     </StackPanel>  
  6. </Grid>  

2. C#

[csharp]  view plain  copy
  1. public partial class MainWindow : Window  
  2. {  
  3.     public MainWindow()  
  4.     {  
  5.         InitializeComponent();  
  6.   
  7.         Binding binding = new Binding("Value") { Source = slider1 };//绑定到slider的Value属性  
  8.   
  9.         /* 
  10.          * UpdateSourceTrigger属性控制绑定源更新的执行时间,默认为LostFocus 
  11.          *  
  12.          * LostFocus: 当TextBox失去焦点时,TextBox输入的内容才会更新源(slider) 
  13.          * PropertyChanged: TextBox输入时,TextBox输入的内容就会更新源(slider) 
  14.          */  
  15.         binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;  
  16.   
  17.         /* 实现数据检测,定义继承ValidationRule的DataValidChcker类,并实例化 */  
  18.         DataValidChcker dataChecker = new DataValidChcker();//绑定“目标”数据有效性检测,TextBox输入数据有效性  
  19.   
  20.         /* 可选,需要检测“源”时设置为true */  
  21.         dataChecker.ValidatesOnTargetUpdated = true;//绑定“源”数据的有效性检测,xaml设置最小值为-10,最大值为110。不在[0, 100]范围将认为无效  
  22.   
  23.         binding.ValidationRules.Add(dataChecker);//Binding有多个数据校验条件  
  24.   
  25.         /* 事件1: 当数据无效时,获取无效信息时设置为true */  
  26.         binding.NotifyOnValidationError = true;//当数据校验为无效时,发出错误事件  
  27.   
  28.         textBox1.SetBinding(TextBox.TextProperty, binding);  
  29.   
  30.         /* 事件2:  当事件1为true时使用 */  
  31.         textBox1.AddHandler(Validation.ErrorEvent, new RoutedEventHandler(DataValidCheckErrorHandler));//当数据校验为无效时,处理事件  
  32.     }  
  33.   
  34.     void DataValidCheckErrorHandler(object sender, RoutedEventArgs arg)  
  35.     {  
  36.         int errorCount = 0;  
  37.   
  38.         ReadOnlyObservableCollection<ValidationError> errors = Validation.GetErrors(textBox1);  
  39.         if (null == errors)  
  40.         {  
  41.             textBox1.ToolTip = "Unknow";  
  42.             return;  
  43.         }  
  44.   
  45.         errorCount = errors.Count;  
  46.   
  47.         if (errorCount > 0)  
  48.         {  
  49.             textBox1.ToolTip = errors[0].ErrorContent.ToString();  
  50.         }  
  51.         else  
  52.         {  
  53.             textBox1.ToolTip = "valid data";  
  54.         }  
  55.     }  
  56. }  
  57.   
  58. public class DataValidChcker : ValidationRule  
  59. {  
  60.     public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)  
  61.     {  
  62.         double data = 0.0;  
  63.   
  64.         if (double.TryParse(value.ToString(), out data))  
  65.         {  
  66.             if (data >= 0 && data <= 100)//有效范围: [0, 100]  
  67.             {  
  68.                 return new ValidationResult(true"data valid");//有效  
  69.             }  
  70.         }  
  71.         return new ValidationResult(false"data invalid");//无效  
  72.     }  
  73. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蚂蚁_CrkRes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值