ExceptionValidation :表示一个规则,该规则检查在绑定源属性更新过程中引发的异常。它是一个内置的规则,它检查在绑定源属性更新过程中引发的异常。
ValidationResult : 数据验证结果的表现方式。ValidationRule对象的Validate方法执行完毕后通过ValidationResult来表示验证的结果。这里包含了错误信息—ErrorContent,数据是否有效—IsValid。ValidResult 为 ValidationResult 的有效实例。
ValidationError :表示一个验证错误,该错误在 ValidationRule 报告验证错误时由绑定引擎创建。
复制代码 代码如下:
XAML:
<Page x:Class="WpfDemo.TypeConvertAndValidationRule"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TypeConvertAndValidationRule"
xmlns:src="clr-namespace:WpfDemo">
<Grid Background="Silver">
<Grid.RowDefinitions>
<RowDefinition>
</RowDefinition>
<RowDefinition>
</RowDefinition>
<RowDefinition>
</RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition>
</ColumnDefinition>
<ColumnDefinition>
</ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="生日" Grid.Row="0" Grid.Column="0"></TextBlock>
<TextBox Grid.Row="0" Grid.Column="1">
<TextBox.Text>
<Binding Path="Birthday" UpdateSourceTrigger="LostFocus" Mode="TwoWay">
<Binding.ValidationRules><src:ValidationDateTimeRule/></Binding.ValidationRules>
<Binding.Converter><src:MyConverterOfBirthFormat/></Binding.Converter>
</Binding>
</TextBox.Text>
<TextBox.ToolTip>
<Binding RelativeSource="{RelativeSource Self}" Path="(Validation.Errors) [0].ErrorContent"></Binding>
</TextBox.ToolTip> </TextBox>
<TextBlock Grid.Row="1" Text="{Binding Path=Birthday,Mode=OneWay}" Grid.Column="1"></TextBlock>
<TextBlock Text="电子邮件格式检查" Grid.Row="2" Grid.Column="0"></TextBlock>
<TextBox Grid.Row="2" Grid.Column="1">
<TextBox.Text>
<Binding Path="EMail">
<Binding.ValidationRules><ExceptionValidationRule /></Binding.ValidationRules>
</Binding> </TextBox.Text>
<TextBox.ToolTip>
<Binding RelativeSource="{RelativeSource Self}" Path="(Validation.Errors)[0].ErrorContent"></Binding>
</TextBox.ToolTip> </TextBox>
</Grid>
</Page>
复制代码 代码如下:
C#:
namespace WpfDemo
{
#region TypeConvertAndValidationRule
public partial class TypeConvertAndValidationRule : Page
{
public TypeConvertAndValidationRule()
{
InitializeComponent();
this.DataContext = new UserInfo { Name = "swd", Birthday =System.Convert.ToDateTime("1987/10/21"), EMail = "swd@126.com" };
}
}
#endregion
#region UserInfo
public class UserInfo
{
#region Name
public string Name
{get;set;}
#endregion
#region Birthday
public DateTime Birthday
{get;set;}
#endregion
#region EMail
private string email;
public string EMail
{
get
{return email;}
set
{
this.email = value;
Regex r = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
if (!r.IsMatch(value))
{
throw new ApplicationException("电子邮件格式有误!");
}
}
}
#endregion
}
#endregion
六、 绑定集合对象
1. ICollectionView接口