The XAML:
DATA INPUT
- <Grid x:Name="LayoutRoot">
- <Grid.Resources>
- <local:Person x:Key="NewJoiner" Age="49" Forename="Fred" Surname="Flintstone" />
- </Grid.Resources>
- <Grid DataContext="{StaticResource NewJoiner}">
- <Grid.ColumnDefinitions>
- <columndefinition Width="1*" />
- <columndefinition Width="5*" />
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <rowdefinition Height="50" />
- <rowdefinition Height="50" />
- <rowdefinition Height="50" />
- </Grid.RowDefinitions>
- <Grid.Resources>
- <Style TargetType="TextBlock">
- <setter Property="Margin" Value="10" />
- <setter Property="HorizontalAlignment" Value="Right" />
- <setter Property="VerticalAlignment" Value="Center" />
- </Style>
- <Style TargetType="TextBox">
- <setter Property="Margin" Value="10" />
- <setter Property="Width" Value="154" />
- <setter Property="HorizontalAlignment" Value="Left" />
- <setter Property="VerticalAlignment" Value="Center" />
- </Style>
- </Grid.Resources>
- <TextBlock Text="First Name" Grid.Row="0" Grid.Column="0" />
- <TextBlock Text="Last Name" Grid.Row="1" Grid.Column="0" />
- <TextBlock Text="Age" Grid.Row="2" Grid.Column="0" />
- <textbox x:Name="Forename" Grid.Row="0" Grid.Column="1" Text="{Binding Forename, Mode=TwoWay, ValidatesOnDataErrors=True}" />
- <textbox x:Name="Surname" Grid.Row="1" Grid.Column="1" Text="{Binding Surname, Mode=TwoWay, ValidatesOnDataErrors=True}" />
- <textbox x:Name="Age" Grid.Row="2" Grid.Column="1" Text="{Binding Age, Mode=TwoWay, ValidatesOnDataErrors=True}" />
- </Grid>
- </Grid>
The code:
IMPLEMENTATION
- public class Person : IDataErrorInfo
- {
- public string Forename { get; set; }
- public string Surname { get; set; }
- public int Age { get; set; }
- public string Error { get { return null; } }
- public string this[string columnName]
- {
- get
- {
- string error = null;
- switch (columnName)
- {
- case "Forename":
- if (string.IsNullOrEmpty(Forename))
- error = "Forename required";
- break;
- case "Surname":
- if (string.IsNullOrEmpty(this.Surname))
- error = "Surname required";
- break;
- case "Age":
- if (this.Age < 0 || this.Age > 130)
- error = "Invalid Age";
- break;
- }
- return error;
- }
- }
- }
The result:
No comments:
Post a Comment