<Window x:Class="BusinessLayerValidation.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF IDataErrorInfo Sample" Width="350" Height="150"
xmlns:src="clr-namespace:BusinessLayerValidation">
<Window.Resources>
<src:Person x:Key="data"/>
<!--The tool tip for the TextBox to display the validation error message.-->
<Style x:Key="textBoxInError" TargetType="TextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid Name="test">
<Border
BorderBrush="#d99"
BorderThickness="2" x:Name="bd">
<ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
</Border>
<Border Name="ErrorImage"
Background="Red" HorizontalAlignment="Right">
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel Margin="20">
<TextBlock>Enter your Name:</TextBlock>
<TextBox Style="{StaticResource textBoxInError}">
<TextBox.Text>
<!-- Setting the ValidatesOnDataErrors to true enables to the Binding to check for
errors raised by the IDataErrorInfo implementation.
Alternatively, you can add DataErrorValidationRule to <Binding.ValidationRules/>-->
<Binding Path="Name" Source="{StaticResource data}"
ValidatesOnDataErrors="True"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<!-- Checks for exceptions during the setting of the source property.
Alternatively, set ValidatesOnExceptions to True on the Binding.-->
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBlock>Enter your age:</TextBlock>
<TextBox Style="{StaticResource textBoxInError}">
<TextBox.Text>
<!-- Setting the ValidatesOnDataErrors to true enables to the Binding to check for
errors raised by the IDataErrorInfo implementation.
Alternatively, you can add DataErrorValidationRule to <Binding.ValidationRules/>-->
<Binding Path="Age" Source="{StaticResource data}"
ValidatesOnDataErrors="True"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<!-- Checks for exceptions during the setting of the source property.
Alternatively, set ValidatesOnExceptions to True on the Binding.-->
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace BusinessLayerValidation
{
public class Person : IDataErrorInfo
{
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
public string Error
{
get
{
return null;
}
}
private string name1;
public string Name
{
get { return name1; }
set { name1 = value; }
}
public string this[string name]
{
get
{
string result = null;
if (name == "Age")
{
if (this.age < 0 || this.age > 150)
{
result = "Age must not be less than 0 or greater than 150.";
}
}
if (name == "Name")
{
if (this.name1 == string.Empty || this.name1 == null)
{
result = "Provide Name";
}
}
return result;
}
}
}
}
No comments:
Post a Comment