Mega Code Archive

 
Categories / C# / WPF
 

Bind to IDataErrorInfo

<Window     x:Class="WpfApplication1.Window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="WPF" Height="136" Width="260">     <Window.Resources>         <Style              x:Key="textBoxInErrorStyle"              TargetType="{x:Type TextBox}" >             <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>             <Setter Property="Validation.ErrorTemplate">                 <Setter.Value>                     <ControlTemplate>                         <DockPanel DockPanel.Dock="Right">                             <AdornedElementPlaceholder/>                             <Image Source="Error.png" Width="16" Height="16" ToolTip="{Binding Path=AdornedElement.ToolTip,                                           RelativeSource={RelativeSource                                              Mode=FindAncestor,                                             AncestorType={x:Type Adorner}}}"/>                         </DockPanel>                     </ControlTemplate>                 </Setter.Value>             </Setter>         </Style>     </Window.Resources>     <StackPanel>         <TextBlock Text="First Name"/>         <TextBox Style="{StaticResource textBoxInErrorStyle}" Text="{Binding Path=FirstName, Mode=TwoWay,                            UpdateSourceTrigger=PropertyChanged,                            ValidatesOnDataErrors=True}" />                  <TextBlock Text="Age" Grid.Row="2" VerticalAlignment="Center"/>         <TextBox Style="{StaticResource textBoxInErrorStyle}"             Margin="4" Text="{Binding Path=Age, Mode=TwoWay,                                    UpdateSourceTrigger=PropertyChanged,                            ValidatesOnDataErrors=True}"/>              </StackPanel> </Window> //File:Window.xaml.cs using System.Windows; using System.ComponentModel; namespace WpfApplication1 {     public partial class Window1 : Window     {         public Window1()         {             InitializeComponent();             this.DataContext = new Employee(){FirstName = "A",Age = 26,};         }     }     public class Employee : INotifyPropertyChanged,IDataErrorInfo     {         private string firstName;         private int age;         public Employee()         {             FirstName = "A";         }         public string FirstName         {             get             {                 return firstName;             }             set             {                 if(firstName != value)                 {                     firstName = value;                     OnPropertyChanged("FirstName");                 }             }         }         public int Age         {             get             {                 return age;             }             set             {                 if(this.age != value)                 {                     this.age = value;                     OnPropertyChanged("Age");                 }             }         }         public event PropertyChangedEventHandler PropertyChanged;         private void OnPropertyChanged(string propertyName){             if(this.PropertyChanged != null)             {                 this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName));             }         }         public string Error         {             get             {                 return string.Empty;             }         }         public string this[string propertyName]         {             get             {                 string message = string.Empty;                 switch(propertyName)                 {                     case "FirstName":                         if(string.IsNullOrEmpty(firstName))                             message = "A person must have a first name.";                         break;                     case "Age":                         if(age < 1)                             message = "A person must have an age.";                         break;                     default:                         break;                 }                 return message;             }         }     } }