.Problem
I take a look the silverlight toolkit ,I found the silverlight 3 have new data validation function.
it is very good function,It support regularExpression in attribute .
Like this:
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Age" });
_Age = value;
OnPropertyChanged("Age");
}
}
}
But I found one problem.
you can take a look my code careful , Age property is Integer .
when I input the data is string ,for example "dddd" ,it don't throw my define the error message,
It throw the message is "input is not correct format" ,I don't know this reason.
Bu I think careful ,it maybe is correct. because I input is string ,C# level will auto throw the error message.
Don't go into my define error message.
If you input the integer more than two length, it will throw my define the error message.
.Solution
you can define the age is string type , It will fixed this problem.
you can follow your state to decide to what to do.
I only advise you to change string type.
sample code:
Person.cs
private int _Age;
[Required]
[RegularExpression(@"^/d{1,2}$", ErrorMessage = "your age must be one or two Integer")]
[Display(Name = "Age", Description = "you should input integter", GroupName = "Age")]
public int Age
{
get { return _Age; }
set
{
if (_Age != value)
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Age" });
_Age = value;
OnPropertyChanged("Age");
}
}
}
private string _Birthday;
[Display(Name = "Birthday", Description = "you inpint format is yyyy-M-d")]
[RegularExpression(@"^/d{4}-/d{1,2}-/d{1,2}$", ErrorMessage = "Birthday is datetime")]
public string Birthday
{
get
{
return _Birthday;
}
set
{
if (_Birthday != value)
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Birthday" });
_Birthday = value;
OnPropertyChanged("Birthday");
}
}
}
/// <summary>
/// Raises a property changed notification for the specified property name.
/// </summary>
/// <param name="propName">The name of the property that changed.</param>
protected virtual void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region IEditableObject Members
private Person cache;
public void BeginEdit()
{
cache.Name = this.Name;
cache.Age = this.Age;
cache.Birthday = this.Birthday;
}
public void CancelEdit()
{
throw new NotImplementedException();
}
public void EndEdit()
{
cache = null;
}
#endregion
}
}
views
<StackPanel Orientation="Horizontal" Grid.Row="2" >
<dataInput:Label Margin="4" Target="{Binding ElementName=Birthday}" Grid.Row="0" HorizontalAlignment="Right" Content="Birthday" />
<TextBox x:Name="Birthday" Text="{Binding Birthday, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Margin="4" Height="25" Width="200" />
<dataInput:DescriptionViewer Margin="4" Target="{Binding ElementName=Birthday}" HorizontalAlignment="Right" Description="Birthday of the Person" />
</StackPanel>
</Grid>
</navigation:Page>
code-behind
//throw new NotImplementedException();
}
// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
}
}