一水寒 发表于 2024-3-4 21:46:44

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

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

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

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

 
Binding的ValidationRules是Collection类型,是一个校验规则列表,可以支持多重校验。而ValidationRule是一个抽象类,所有要实现业务规则校验,就要继承ValidationRule并实现抽象方法。
ValidationRule的Validate方法返回一个ValidationResult对象,如果校验通过,则ValidationResult的IsValid为true,否则为false。RangeValidationRule主要校验用户输入数据的范围:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace WpfApp4.DataValidate
{
    public class RangeValidationRule : ValidationRule
    {
      public override ValidationResult Validate(object value, CultureInfo cultureInfo)
      {
            double d = 0;
            if(double.TryParse(value.ToString(),out d))
            {
                if(d>=0 && d <= 100)
                {
                  return new ValidationResult(true,null);
                }
            }
            return new ValidationResult(false, "数据必须在0~100之间");
      }
    }

4. 数据校验示例演示

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

 
数据类型转换

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

 
Binding的Converter属性是IValueConverter类型,所以定义转换器需要实现IValueConverter接口,如下所示:
<TextBlock Text="年龄" VerticalAlignment="Center"></TextBlock>
<TextBox x:Name="tbAge" Width="120" Height="25" Margin="10" Validation.Error="tbAge_Error" VerticalContentAlignment="Center">
private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}<TextBox.Text>
private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}<Binding Path="Age" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}<Binding.ValidationRules>
private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}<v:RangeValidationRule ValidatesOnTargetUpdated="True"></v:RangeValidationRule>
private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}</Binding.ValidationRules>
private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}</Binding>
private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}</TextBox.Text>
</TextBox>
<TextBlock VerticalAlignment="Center" Text="{Binding DataError}" Foreground="Red"></TextBlock>注意:IValueConverter接口共两个方法,其中Convert方法当Source到Target时调用,ConvertBack方法则是反方向调用。
 
2. 定义资源

 
转换器也是一种资源,要声明资源,首先引入命名空间xmlns:c="clr-namespace:WpfApp4.DataConverter",如下所示:
private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);

3. 调用类型转换

 
在Binding时,调用资源Visibility="{Binding ElementName=chk01, Path=IsChecked, Converter={StaticResource boolToVisility}}"。通过复选框的选择与否,控制控件的显示与隐藏,如下所示:
private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);
}private void tbAge_Error(object sender, ValidationErrorEventArgs e)
{
        this.viewModel.ValidationErrorCommand.Execute(e);

4. 数据类型转换示例演示

 

 
源码下载

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

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

来源:https://www.cnblogs.com/hsiang/p/18050883
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 浅谈WPF之Binding时数据校验和类型转换