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. ArcMobile的CoordinateCollection在逆时针添加点时自动调整节点顺序的问题

    为了使用ArcMobile实现量测功能,LZ自定义了一个MapGraphicLayer用于绘图,代码如下: using System.Drawing; using ESRI.ArcGIS.Mobile ...

  2. docker的网络-单主机(三种原生网络)none、host、bridge

    docker的网络分为:单主机.跨主机 这篇先说:单主机 我们先说一下docker的原生网络模式 网络模式 简介 优点 使用场景 none 空网络,没有网络 此网络与外界隔离,安全度非常高 适合公司内 ...

  3. Python-collections模块之defaultdict

    defaultdict defaultdict 是 dict 类型的子类,正如其名,初始化时,可以给key指定默认值,什么意思呢?直接看代码.如果是普通的dict对象,访问一个不存在的key时,会报错 ...

  4. codeforces 6D

    D. Lizards and Basements 2 time limit per test 2 seconds memory limit per test 64 megabytes input st ...

  5. HDU 4649 Professor Tian(概率DP)题解

    题意:一个表达式,n + 1个数,n个操作,每个操作Oi和数Ai+1对应,给出每个操作Oi和数Ai+1消失的概率,给出最后表达式值得期望.只有| , ^,&三个位操作 思路:显然位操作只对当前 ...

  6. Javascript实现"点按钮出随机背景色的"三个DIV

    <!DOCTYPE html> <html> <head> <title>Random_Color-Transformation</title&g ...

  7. JavaScript 的 7 种设计模式

    原文地址:Understanding Design Patterns in JavaScript 原文作者:Sukhjinder Arora 译者:HelloGitHub-Robert 当启动一个新的 ...

  8. 加密算法大全图解 :密码体系,对称加密算法,非对称加密算法,消息摘要, Base64,数字签名,RSA,DES,MD5,AES,SHA,ElGamal,

    1. 加密算法大全: ***************************************************************************************** ...

  9. Cookie 政策

    Cookie 政策 合规/隐私协议 https://www.synology.cn/zh-cn/company/legal/cookie_policy Cookie Cookie 政策 生效日期:20 ...

  10. linux DRM 之 GEM 笔记

    原文链接:https://www.cnblogs.com/yaongtime/p/14418357.html 在GPU上的各类操作中涉及到多种.多个buffer的使用. 通常我们GPU是通过图像API ...