WPF-Binding对数据的检验

设置Binding的ValidationRules属性对Binding进行检验

<StackPanel>
<TextBox x:Name="txtAge" FontSize="30" Foreground="Red"></TextBox>
<TextBlock x:Name="errorSummary" FontSize="30" Foreground="Red"></TextBlock>
</StackPanel>

后台代码

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Person p = new Person { Age = 20, Name = "Tom" };
Binding binding = new Binding("Age") { Source = p };
binding.NotifyOnValidationError = true;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
RangeValidationRule rv = new RangeValidationRule();
binding.ValidationRules.Add(rv);
this.txtAge.SetBinding(TextBox.TextProperty, binding);
this.txtAge.AddHandler(Validation.ErrorEvent, new RoutedEventHandler(this.ValidationError));
}
void ValidationError(object sender, RoutedEventArgs e)
{
if (Validation.GetErrors(this.txtAge).Count > 0)
{
this.txtAge.ToolTip = Validation.GetErrors(this.txtAge)[0].ErrorContent.ToString();
this.errorSummary.Text = Validation.GetErrors(this.txtAge)[0].ErrorContent.ToString();
// You can do everything here when validation error occurs
}
}
}
}

同样,我们在XAML里也可以设置验证

<StackPanel>
<TextBox x:Name="txtAge" FontSize="30" Foreground="Red" Validation.Error="txtAge_Error">
<Binding NotifyOnValidationError="True" Path="Age" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:RangeValidationRule></local:RangeValidationRule>
</Binding.ValidationRules>
</Binding>
</TextBox>
<TextBlock x:Name="errorSummary" FontSize="30" Foreground="Red"></TextBlock>
</StackPanel>

后台代码:

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Person p = new Person { Age = 20, Name = "Tom" };
this.DataContext = p;
}
private void txtAge_Error(object sender, ValidationErrorEventArgs e)
{
if (Validation.GetErrors(this.txtAge).Count > 0)
{
this.txtAge.ToolTip = Validation.GetErrors(this.txtAge)[0].ErrorContent.ToString();
this.errorSummary.Text = Validation.GetErrors(this.txtAge)[0].ErrorContent.ToString();
// You can do everything here when validation error occurs
}
}
}

时间: 2024-08-10 15:01:43

WPF-Binding对数据的检验的相关文章

.NET: WPF Binding对数据的校验和转换

一.校验 一般需要对target上的值进行校验. xaml: 1 <Window x:Class="WpfApplication1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="Simpl

WPF Data Binding之数据的转换和校验【四】

Binding的作用就是架在Source和Target之间的桥梁,数据可以在这座桥梁的帮助下来流通.就像现实社会中桥梁需要设置安检和关卡一样,Binding这座桥上也可以设置关卡对数据进行验证,不仅如此,如果Binding两端需要不同的数据类型的时候我们还可以为数据设置转换器. Binding用于数据有效性校验的关卡是他的ValidationRules属性,用于数据类型转换的关卡是它的Convert属性. 1.1 Binding的数据校验 Binding的ValidationRules属性是Co

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

Binding在Slider控件与TextBox控件之间建立关联,值可以互相绑定,但是它们的数据类型是不同的,Slider是Double类型,Text为String.原来,Binding有一种机制称为数据转换(Data Converter),当数据绑定的源与目标不同类型时,处理比较简单时,系统就自动的进行了类型转换,但是对于相对复杂的类型转换时,就需要我们手动进行了. 下面用一个例子来说明Convert的应用,程序的用途是在列表里面向玩家显示一些球的状态. 首先创建几个自定义数据类型: publ

WPF Binding学习(二)

Binding作为数据的桥梁,连通业务逻辑层的对象(源对象)和UI的控件对象(目标对象).在这座桥梁上,我们不仅可以控制在源对象与目标对象是双向通行还是单向通行.还可以控制数据的放行时机,甚至可以在这座桥上搭建一些关卡用来转换数据类型或者检验数据的正确性 我们先做一个最基本的例子, 创建一个"Student"类,这个类的实例将作为数据源来使用 public class Student { private int _id; public int ID { get { return _id

WPF入门教程系列(二) 深入剖析WPF Binding的使用方法

WPF入门教程系列(二) 深入剖析WPF Binding的使用方法 同一个对象(特指System.Windows.DependencyObject的子类)的同一种属性(特指DependencyProperty)只能拥有一个binding. 这一点可以通过设置binding对象的方法名得知: public static BindingExpressionBase SetBinding( DependencyObject target, DependencyProperty dp, BindingB

WPF中的数据模板(DataTemplate)(转)

原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/30/694388.html WPF中的数据模板(DataTemplate)                                                                                                                          周银辉 在WPF中我们可以为自己的数据定制显示方式,也就是说虽然某数据

WPF 精修篇 数据触发器

原文:WPF 精修篇 数据触发器 数据触发器 可以使用Binding 来绑定控件 或者数据源 来触发相关动作 举栗子 <Window.Resources> <Style TargetType="{x:Type Label}"> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=red,Path=IsChecked}" Value="True&qu

WPF DataGrid 绑定数据及时更新的处理

原文:WPF DataGrid 绑定数据及时更新的处理 默认情况下datagrid 绑定数据源后,在界面编辑某一列后,数据不会及时更新到内存对象中.如在同一行上有一个命令对来获取 当前选中行(内存对象)发现,数据未更新过来. 解决办法: 在列的绑定属性里加上UpdateSourceTrigger,示例XAML如下 <DataGrid Name="dgProducts" IsReadOnly="False" CanUserAddRows="False&

WPF Binding值转换器ValueConverter使用简介(二)-IMultiValueConverter

注: 需要继承IMultiValueConverter接口,接口使用和IValueConverter逻辑相同. 一.MultiBinding+Converter 多值绑定及多值转换实例 当纵向流量大于横向流量时指示灯应为绿色,当纵向流量小于横向流量时指示灯应为红色,否则指示灯为黄色. 1.定制ColorConverter类,此时Convert中参数是object[] values,values[0]对应MultiBinding中的第一个Binding值,这里是纵向流量值,依此类推,可以在Mult

C# net winform wpf 发送post数据和xml到网页

由于项目需要发送数据到网页 这里用aspx做测试 采用post以及get发送数据,页面进行数据  首先这个东西很简单很简单,基本上学过的都会,但是原谅一直搞cs几乎不搞bs的猿类吧.三四年没接触bs. 除了记录自己用意外.也希望能帮助别人 写程序与bs交互的处理webservice也没别的了.冷不丁的遇到了还抓瞎,只能网上搜罗然后测试在整理,在学习....... 希望帮助没接触过的,以及也是一直搞cs的猿类,请bs大神,高手,给出更多的解决方案 首先采用最简单的微软子提供的方式 aspx可以用r