Monday, December 14, 2009

Five Minute Silverlight 4 Aides-Memoire #7 – IDataErrorInfo

The XAML:

DATA INPUT
  1. <Grid x:Name="LayoutRoot">
  2.   <Grid.Resources>
  3.     <local:Person x:Key="NewJoiner" Age="49" Forename="Fred" Surname="Flintstone" />
  4.   </Grid.Resources>
  5.   <Grid DataContext="{StaticResource NewJoiner}">
  6.     <Grid.ColumnDefinitions>
  7.       <columndefinition Width="1*" />
  8.       <columndefinition Width="5*" />
  9.     </Grid.ColumnDefinitions>
  10.     <Grid.RowDefinitions>
  11.       <rowdefinition Height="50" />
  12.       <rowdefinition Height="50" />
  13.       <rowdefinition Height="50" />
  14.     </Grid.RowDefinitions>
  15.     <Grid.Resources>
  16.       <Style TargetType="TextBlock">
  17.         <setter Property="Margin" Value="10" />
  18.         <setter Property="HorizontalAlignment" Value="Right" />
  19.         <setter Property="VerticalAlignment" Value="Center" />
  20.       </Style>
  21.       <Style TargetType="TextBox">
  22.         <setter Property="Margin" Value="10" />
  23.         <setter Property="Width" Value="154" />
  24.         <setter Property="HorizontalAlignment" Value="Left" />
  25.         <setter Property="VerticalAlignment" Value="Center" />
  26.       </Style>
  27.     </Grid.Resources>
  28.     <TextBlock Text="First Name" Grid.Row="0" Grid.Column="0" />
  29.     <TextBlock Text="Last Name" Grid.Row="1" Grid.Column="0" />
  30.     <TextBlock Text="Age" Grid.Row="2" Grid.Column="0" />
  31.     <textbox x:Name="Forename" Grid.Row="0" Grid.Column="1" Text="{Binding Forename, Mode=TwoWay, ValidatesOnDataErrors=True}" />
  32.     <textbox x:Name="Surname" Grid.Row="1" Grid.Column="1" Text="{Binding Surname, Mode=TwoWay, ValidatesOnDataErrors=True}" />
  33.     <textbox x:Name="Age" Grid.Row="2" Grid.Column="1" Text="{Binding Age, Mode=TwoWay, ValidatesOnDataErrors=True}" />
  34.   </Grid>
  35. </Grid>

The code:

IMPLEMENTATION
  1. public class Person : IDataErrorInfo
  2. {
  3.     public string Forename { get; set; }
  4.     public string Surname { get; set; }
  5.     public int Age { get; set; }
  6.     public string Error { get { return null; } }
  7.  
  8.     public string this[string columnName]
  9.     {
  10.         get
  11.         {
  12.             string error = null;
  13.             switch (columnName)
  14.             {
  15.                 case "Forename":
  16.                     if (string.IsNullOrEmpty(Forename))
  17.                         error = "Forename required";
  18.                     break;
  19.                 case "Surname":
  20.                     if (string.IsNullOrEmpty(this.Surname))
  21.                         error = "Surname required";
  22.                     break;
  23.                 case "Age":
  24.                     if (this.Age < 0 || this.Age > 130)
  25.                         error = "Invalid Age";
  26.                     break;
  27.             }
  28.             return error;
  29.         }
  30.     }
  31. }


The result:

image

1 comment:

GIGI said...

困難的不在於新概念,而在於逃避舊有的概念。.........................