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);

拓展:

  1. RangeValidationRule 默认只检验从 Target 回 Source 的数据传递,默认从 Source 传过来的数据是合法的,如果想校验从 Source 传过来的数据,需设置 rvr.ValidatesOnTargetUpdated = true;
  2. 如果想监听校验失败的事件,设置 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}}"/>

WPF 基础 - Binding 对数据的转换和校验的更多相关文章

  1. WPF之Binding对数据的转换(第五天)

    Binding在Slider控件与TextBox控件之间建立关联,值可以互相绑定,但是它们的数据类型是不同的,Slider是Double类型,Text为String.原来,Binding有一种机制称为 ...

  2. WPF 之 Binding 对数据的校验与转换(三)

    一.前言 ​ Binding 的作用就是架在 Source 和 Target 之间的桥梁,数据可以在这座桥梁的帮助下来流通.就像现实中的桥梁会设置一些关卡进行安检一样,Binding 这座桥上也可以设 ...

  3. WPF 基础 - Binding 的源与路径

    1. 源与路径 把控件作为 binding 源与 binding 标记拓展: 控制 Binding 的方向及数据更新: Binding 的路径 Path: 没有路径的 Binding: 为 Bindi ...

  4. WPF 基础 - Binding 的 数据更新提醒

    WPF 作为一个专门的展示层技术,让程序员专注于逻辑层,让展示层永远处于逻辑层的从属地位: 这主要因为有 DataBinding 和配套的 Dependency Property 和 DataTemp ...

  5. WPF之Binding深入探讨

    原文:http://blog.csdn.net/fwj380891124/article/details/8107646 1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在 ...

  6. WPF的Binding功能解析

    1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...

  7. WPF之Binding深入探讨--Darren

    1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...

  8. WPF之Binding【转】

    WPF之Binding[转] 看到WPF如此之炫,也想用用,可是一点也不会呀. 从需求谈起吧: 首先可能要做一个很炫的界面.见MaterialDesignInXAMLToolKit. 那,最主要的呢, ...

  9. WPF之Binding深入探讨 转载:http://blog.csdn.net/fwj380891124/article/details/8107646

    1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...

随机推荐

  1. Python 遭遇 ProxyError 问题记录

    最近遇到的一个问题,在搞清楚之后才发现这么多年的 HTTPS_PROXY 都配置错了! 起因 想用 Python 在网上下载一些图片素材,结果 requests 报错 requests.excepti ...

  2. [Python] Pandas 中 Series 和 DataFrame 的用法笔记

    目录 1. Series对象 自定义元素的行标签 使用Series对象定义基于字典创建数据结构 2. DataFrame对象 自定义行标签和列标签 使用DataFrame对象可以基于字典创建数据结构 ...

  3. MySQL 事务日志

    重做日志(Redo log) 重做日志(Redo log),也叫做前滚日志,存放在如下位置,轮询使用,记录着内存中数据页的变化,在事务 ACID 过程中,主要实现的是 D(Durability)的作用 ...

  4. js binary search algorithm

    js binary search algorithm js 二分查找算法 二分查找, 前置条件 存储在数组中 有序排列 理想条件: 数组是递增排列,数组中的元素互不相同; 重排 & 去重 顺序 ...

  5. Event Bus & Event Emitter

    Event Bus & Event Emitter Event Bus https://code.luasoftware.com/tutorials/vuejs/parent-call-chi ...

  6. ts 修改readonly参数

    readonly name = "xxx"; updateValueAndValidity(): void { // this.name = 'a'; (this as { nam ...

  7. 星盟全球投资副总裁DENIEL SOIBIM:如何激发创造力

    丹尼尔·索比姆毕业于加州理工大学,2005年通过创建投资俱乐部对潜力公司进行天使投资,获得了美国Blue Run高层的重视,任营收专家评估师,为Blue Run项目提案做风险评估,09年与泰勒·亚当斯 ...

  8. NGK 路演美国站,SPC空投与NGK项目安全

    最近,NGK全球巡回路演在美国最大城市纽约市落下帷幕,本次路演有幸邀请了NGK方面代表迈尔逊,纽约当地区块链大咖维克多以及美国当地社群意见代表乔治等人. 路演一开始,美国当地路演师Viko首先致辞,V ...

  9. NDK android Error:Expected caller to ensure valid ABI: MIPS

    android studio 安装NDK之后,报错 Error:Expected caller to ensure valid ABI: MIPS 环境: android studio 2.3 gra ...

  10. 【HTB系列】靶机Access的渗透测试详解

    出品|MS08067实验室(www.ms08067.com) 本文作者:大方子(Ms08067实验室核心成员) Hack The Box是一个CTF挑战靶机平台,在线渗透测试平台.它能帮助你提升渗透测 ...