翼度科技»论坛 编程开发 .net 查看内容

浅谈WPF之Binding时数据校验和类型转换

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
在WPF开发中,Binding实现了数据在Source和Target之间的传递和流通,就像现实生活中的一条条道路,建立起了城镇与城镇之间的衔接,而数据校验和类型转换,就像高速公路之间的收费站和安检站。那在WPF开发中,如何实现数据的校验和类型转换呢?本文以一个简单的小例子,简述在WPF开发中,实现数据校验和类型转换的相关知识点,仅供学习分享使用,如有不足之处,还请指正。

在数据绑定Binding中,数据校验主要通过ValidationRules属性,数据转换主要通过Converter属性
 
数据校验

 
在WPF开发中,校验数据的有效性,主要步骤如下:
 
1. 实现校验规则

 
Binding的ValidationRules是Collection类型,是一个校验规则列表,可以支持多重校验。而ValidationRule是一个抽象类,所有要实现业务规则校验,就要继承ValidationRule并实现抽象方法。
ValidationRule的Validate方法返回一个ValidationResult对象,如果校验通过,则ValidationResult的IsValid为true,否则为false。RangeValidationRule主要校验用户输入数据的范围:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Controls;
  8. namespace WpfApp4.DataValidate
  9. {
  10.     public class RangeValidationRule : ValidationRule
  11.     {
  12.         public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  13.         {
  14.             double d = 0;
  15.             if(double.TryParse(value.ToString(),out d))
  16.             {
  17.                 if(d>=0 && d <= 100)
  18.                 {
  19.                     return new ValidationResult(true,null);
  20.                 }
  21.             }
  22.             return new ValidationResult(false, "数据必须在0~100之间");
  23.         }
  24.     }
  25. }
复制代码
 
4. 数据校验示例演示

 
经过上述步骤,运行程序,如下所示:

 
数据类型转换

 
在数据绑定时,如果Source端的数据类型和Target端的数据类型不一致时,就需要用到数据类型转换,如:true/false与显示/隐藏之间的转换等,要实现数据转换,需要用到Binding的Converter属性。步骤如下:
 
1. 定义转换器

 
Binding的Converter属性是IValueConverter类型,所以定义转换器需要实现IValueConverter接口,如下所示:
  1. <TextBlock Text="年龄" VerticalAlignment="Center"></TextBlock>
  2. <TextBox x:Name="tbAge" Width="120" Height="25" Margin="10" Validation.Error="tbAge_Error" VerticalContentAlignment="Center">
  3. private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  4. {
  5.         this.viewModel.ValidationErrorCommand.Execute(e);
  6. }<TextBox.Text>
  7. private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  8. {
  9.         this.viewModel.ValidationErrorCommand.Execute(e);
  10. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  11. {
  12.         this.viewModel.ValidationErrorCommand.Execute(e);
  13. }<Binding Path="Age" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
  14. private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  15. {
  16.         this.viewModel.ValidationErrorCommand.Execute(e);
  17. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  18. {
  19.         this.viewModel.ValidationErrorCommand.Execute(e);
  20. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  21. {
  22.         this.viewModel.ValidationErrorCommand.Execute(e);
  23. }<Binding.ValidationRules>
  24. private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  25. {
  26.         this.viewModel.ValidationErrorCommand.Execute(e);
  27. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  28. {
  29.         this.viewModel.ValidationErrorCommand.Execute(e);
  30. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  31. {
  32.         this.viewModel.ValidationErrorCommand.Execute(e);
  33. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  34. {
  35.         this.viewModel.ValidationErrorCommand.Execute(e);
  36. }<v:RangeValidationRule ValidatesOnTargetUpdated="True"></v:RangeValidationRule>
  37. private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  38. {
  39.         this.viewModel.ValidationErrorCommand.Execute(e);
  40. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  41. {
  42.         this.viewModel.ValidationErrorCommand.Execute(e);
  43. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  44. {
  45.         this.viewModel.ValidationErrorCommand.Execute(e);
  46. }</Binding.ValidationRules>
  47. private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  48. {
  49.         this.viewModel.ValidationErrorCommand.Execute(e);
  50. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  51. {
  52.         this.viewModel.ValidationErrorCommand.Execute(e);
  53. }</Binding>
  54. private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  55. {
  56.         this.viewModel.ValidationErrorCommand.Execute(e);
  57. }</TextBox.Text>
  58. </TextBox>
  59. <TextBlock VerticalAlignment="Center" Text="{Binding DataError}" Foreground="Red"></TextBlock>
复制代码
注意:IValueConverter接口共两个方法,其中Convert方法当Source到Target时调用,ConvertBack方法则是反方向调用。
 
2. 定义资源

 
转换器也是一种资源,要声明资源,首先引入命名空间xmlns:c="clr-namespace:WpfApp4.DataConverter",如下所示:
  1. private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  2. {
  3.         this.viewModel.ValidationErrorCommand.Execute(e);
  4. }
复制代码
 
3. 调用类型转换

 
在Binding时,调用资源Visibility="{Binding ElementName=chk01, Path=IsChecked, Converter={StaticResource boolToVisility}}"。通过复选框的选择与否,控制控件的显示与隐藏,如下所示:
  1. private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  2. {
  3.         this.viewModel.ValidationErrorCommand.Execute(e);
  4. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  5. {
  6.         this.viewModel.ValidationErrorCommand.Execute(e);
  7. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  8. {
  9.         this.viewModel.ValidationErrorCommand.Execute(e);
  10. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  11. {
  12.         this.viewModel.ValidationErrorCommand.Execute(e);
  13. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  14. {
  15.         this.viewModel.ValidationErrorCommand.Execute(e);
  16. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  17. {
  18.         this.viewModel.ValidationErrorCommand.Execute(e);
  19. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  20. {
  21.         this.viewModel.ValidationErrorCommand.Execute(e);
  22. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  23. {
  24.         this.viewModel.ValidationErrorCommand.Execute(e);
  25. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  26. {
  27.         this.viewModel.ValidationErrorCommand.Execute(e);
  28. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  29. {
  30.         this.viewModel.ValidationErrorCommand.Execute(e);
  31. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  32. {
  33.         this.viewModel.ValidationErrorCommand.Execute(e);
  34. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  35. {
  36.         this.viewModel.ValidationErrorCommand.Execute(e);
  37. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  38. {
  39.         this.viewModel.ValidationErrorCommand.Execute(e);
  40. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  41. {
  42.         this.viewModel.ValidationErrorCommand.Execute(e);
  43. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  44. {
  45.         this.viewModel.ValidationErrorCommand.Execute(e);
  46. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  47. {
  48.         this.viewModel.ValidationErrorCommand.Execute(e);
  49. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  50. {
  51.         this.viewModel.ValidationErrorCommand.Execute(e);
  52. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  53. {
  54.         this.viewModel.ValidationErrorCommand.Execute(e);
  55. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  56. {
  57.         this.viewModel.ValidationErrorCommand.Execute(e);
  58. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  59. {
  60.         this.viewModel.ValidationErrorCommand.Execute(e);
  61. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  62. {
  63.         this.viewModel.ValidationErrorCommand.Execute(e);
  64. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  65. {
  66.         this.viewModel.ValidationErrorCommand.Execute(e);
  67. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  68. {
  69.         this.viewModel.ValidationErrorCommand.Execute(e);
  70. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  71. {
  72.         this.viewModel.ValidationErrorCommand.Execute(e);
  73. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  74. {
  75.         this.viewModel.ValidationErrorCommand.Execute(e);
  76. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  77. {
  78.         this.viewModel.ValidationErrorCommand.Execute(e);
  79. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  80. {
  81.         this.viewModel.ValidationErrorCommand.Execute(e);
  82. }private void tbAge_Error(object sender, ValidationErrorEventArgs e)
  83. {
  84.         this.viewModel.ValidationErrorCommand.Execute(e);
  85. }
复制代码
 
4. 数据类型转换示例演示

 

 
源码下载

 
本实例源码相对简单,如果需要源码下载,可关注公众号,回复关键字DATAVC进行获取,如下所示:

以上就是【浅谈WPF之Binding时数据校验和类型转换】的全部内容,关于更多详细内容,可参考官方文档。希望能够一起学习,共同进步。

来源:https://www.cnblogs.com/hsiang/p/18050883
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具