How do i catch the validation from the DataAnnotations ? i research here but i didn't understand how it works
so i hoppe some of you can enlighten my
here my current test code:
Model
public class Person // Represents person data.
{
/// <summary>
/// Gets or sets the person's first name.
/// </summary>
/// <remarks>
/// Empty string or null are not allowed.
/// Allow minimum of 2 and up to 40 uppercase and lowercase.
/// </remarks>
[Required]
[RegularExpression(@"^[a-zA-Z''-'\s]{2,40}$")]
public string FirstName{ get; set;}
/// <summary>
/// Gets or sets the person's last name.
/// </summary>
/// <remarks>
/// Empty string or null are not allowed.
/// </remarks>
[Required]
public string LastName { get; set;}
public int Age{ get; set;}
}
View
<Window x:Class="DataAnnotationstest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataAnnotationstest"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:Person FirstName="Tomer" LastName="Shamam" />
</Window.DataContext>
<Grid>
<StackPanel Margin="4,4,51,4">
<TextBox Text="{Binding FirstName, ValidatesOnDataErrors=True}" />
<TextBox Text="{Binding LastName, ValidatesOnDataErrors=True}" />
<TextBox Text="{Binding Age, ValidatesOnDataErrors=True}" />
</StackPanel>
</Grid>
</Window>
do i need to implement something else to Person? i found here the following code but like i said before i didn't understand how it's worke -.-
public static T GetAttributeFrom<T>(this object instance, string propertyName) where T : Attribute
{
var attrType = typeof(T);
var property = instance.GetType().GetProperty(propertyName);
return (T)property .GetCustomAttributes(attrType, false).First();
}