I have applied the validations of required field validation on the wpf textbox, using the ValidationRule class. My code is :
public class RequiredField : ValidationRule
{
private String _errorMessage = String.Empty;
public string ErrorMessage
{
get { return _errorMessage; }
set { _errorMessage = value; }
}
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var str = value as string;
if (String.IsNullOrEmpty(str))
{
return new ValidationResult(false, this.ErrorMessage);
}
return new ValidationResult(true, null);
}
}
And the XAML code is below :
<TextBox Name="txtName" MaxLength="50">
<TextBox.Text>
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<myValidtaion:RequiredField ErrorMessage="Please enter Name." />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
This seems to work fine. But the issue is that after i save the valid data in database and clear the database, this validation is fired again and validation message appears again.
How can I avoid this situation ?