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. GO - 高级编程

    https://books.studygolang.com/gopl-zh/ https://chai2010.cn/advanced-go-programming-book/

  2. WSL (Windows Subsystem for Linux) 的 VSLAM (Visual Simultaneous Localization and Mapping) 道路

    WSL 的 VSLAM 道路 以 Windows Subsystem for Linux 闯入 Visual Simultaneous Localization and Mapping 世界的艰难道路 ...

  3. Butterfly美化

    Butterfly美化 首先提示,本文量特别大哦!基本上有所有的美化,还在持续更新ing,谨慎入坑......... 主题配置文件修改 基础配置 最最最开始的,好不容易搭建了自己的个人博客,当然要写上 ...

  4. spyder import tensorflow

    之前安装了tensorflowgpu,但是在spyder中import会失败. 原因是因为新建了环境,要activate tensorflow-gpu进入安装了tensorflow 的环境才可以imp ...

  5. codevs1039整数的k划分-思考如何去重复

    题目描述将整数n分成k份,且每份不能为空,任意两种划分方案不能相同(不考虑顺序).例如:n=7,k=3,下面三种划分方案被认为是相同的.1 1 51 5 15 1 1问有多少种不同的分法.输入描述输入 ...

  6. CSS Grid & Flex poster PDF 海报定制

    CSS Grid & Flex poster PDF 海报定制 CSS 手工实现 导出 SVG / PNG 导出 PDF 打印,定制海报 refs https://css-tricks.com ...

  7. config file language All In One

    config file language All In One YAML YAML Ain't Markup Language .yaml / .yml https://yaml.org/ https ...

  8. Proxifier

    Proxifier 使用教程 https://www.proxifier.com/ Proxifier允许不支持通过代理服务器工作的网络应用程序通过SOCKS或HTTPS代理和链进行操作. confi ...

  9. Flutter DraggableScrollableSheet 可滚动对象的容器

    文档 Example import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp ex ...

  10. django学习-18.*args和**kwargs的用法和使用场景

    目录结构 1.前言 2.[*args]的用法 2.1.第一步:首先编写这样的函数[test1]. 2.2.第二步:给函数[test1]赋值相关入参值. 2.3.第三步:调用函数[test1],得到以下 ...