WPF 基础 - Binding 对数据的转换和校验
1. Binding 对数据的转换和校验
Binding 中,有检验和转换关卡。
1.1 数据校验
源码:
namespace System.Windows.Data
{
public class Binding : BindingBase
{
...
public Collection<ValidationRule> ValidationRules { get; }
...
}
}
namespace System.Windows.Controls
{
//
// 摘要:
// 提供创建自定义规则的一个方式,旨在检查用户输入的有效性。
public abstract class ValidationRule
{
public abstract ValidationResult Validate(object value, CultureInfo cultureInfo);
}
//
// 摘要:
// 表示 ValidationRule.Validate(Object, CultureInfo)方法返回的结果。
public class ValidationResult
{
public bool IsValid { get; }
public object ErrorContent { get; }
}
}
实例:
<Slider x:Name="sliderA" Minimum="0" Maximum="100"/>
<TextBox x:Name="textBoxA"/>
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 <= 10)
{
return new ValidationResult(true, null);
}
}
return new ValidationResult(false, "Validation Failed");
}
}
RangeValidationRule rvr = new RangeValidationRule();
Binding bindingS = new Binding("Value") { Source = this.sliderA };
bindingS.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
bindingS.ValidationRules.Add(rvr);
this.textBoxA.SetBinding(TextBox.TextProperty, bindingS);
拓展:
- RangeValidationRule 默认只检验从 Target 回 Source 的数据传递,默认从 Source 传过来的数据是合法的,如果想校验从 Source 传过来的数据,需设置 rvr.ValidatesOnTargetUpdated = true;
- 如果想监听校验失败的事件,设置 bindingS.NotifyOnValidationError = true,并为路由事件指定处理程序。
源码:
namespace System.Windows.Controls
{
public static class Validation
{
// 校验失败时触发的事件
public static readonly RoutedEvent ErrorEvent;
}
//
// 摘要:
// 表示一个验证错误,该错误可通过 System.Windows.Controls.ValidationRule 报告验证错误时由绑定引擎创建
public class ValidationError
{
public object ErrorContent { get; set; }
}
}
实例:
RangeValidationRule rvr = new RangeValidationRule();
rvr.ValidatesOnTargetUpdated = true;
bindingS.ValidationRules.Add(rvr);
bindingS.NotifyOnValidationError = true;
this.textBoxA.SetBinding(TextBox.TextProperty, bindingS);
this.textBoxA.AddHandler(Validation.ErrorEvent, new RoutedEventHandler(this.ValidationError));
private void ValidationError(object sender, RoutedEventArgs e)
{
if (Validation.GetErrors(this.textBoxA).Count > 0)
{
MessageBox.Show(Validation.GetErrors(this.textBoxA)[0].ErrorContent.ToString();
}
}
1.2 数据转换
namespace System.Windows.Data
{
public interface IValueConverter
{
// Source To Target
object Convert(object value, Type targetType, object parameter, CultureInfo culture);
// Target To Source
object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
}
}
public enum Category
{
Bomber,
Fighter
}
public class CategoryToSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Category c = (Category)value;
switch (c)
{
case Category.Bomber:
return @"llcons\Bomber.png";
case Category.Fighter:
return @"\lcoos\Fighter.png";
default:
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Window.Resources>
<local:CategoryToSourceConverter x:Key="cts"/>
</Window.Resources>
<Image Widlh="20" Height="20"
Source="{Binding Path=Category, Converter={StaticResoun cts}}"/>