Skip to content

Instantly share code, notes, and snippets.

@nseba
Created October 12, 2012 10:51
Show Gist options
  • Save nseba/3878667 to your computer and use it in GitHub Desktop.
Save nseba/3878667 to your computer and use it in GitHub Desktop.
Xaml and view model for validation
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Style="{StaticResource FormLabelStyle}" Text="Name"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Name, Mode=TwoWay}" common:Validation.Property="Name" common:Validation.ValidationPlaceholder="{Binding ElementName=NameValidation}" common:Validation.DataContext="{Binding}" />
<View:ValidationResultPlaceholder Grid.Row="0" x:Name="NameValidation" Grid.Column="2" />
</Grid>
using System;
using System.ComponentModel.DataAnnotations;
internal class SampleViewModel : ViewModel
{
public SampleViewModel()
{
ValidateObject();
}
[Required]
[StringLength(250)]
public string Name
{
get
{
return name;
}
set
{
SetPropertyAndValidate(() => name, x => name = x, value);
}
}
[Required]
[StringLength(250)]
public string Address
{
get
{
return address;
}
set
{
SetPropertyAndValidate(() => address, x => address = x, value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment