Textbox validation
-
Can any one give me idea how i can validate a texbox. I mean user should enter some thing in the textbox(Like Required field validation in asp.net). Please give me a simple example.
Well - there are many ways to perform validation in WPF. Here's an example of a validation rule that ensures that your input matches a particular format. The first part is a C# class:
public MatchRule : ValidationRule
{
public string MatchText { get; set; }public override ValidationResult Validate(object value,
CultureInfo info)
{
if ((string)value == MatchText)
return new ValidationResult(true, null);
return new ValidationResult(false, "This is not a match");
}
}and in the XAML:
<TextBox>
<TextBox.Text>
<Binding.ValidationRules>
<local:MatchRule MatchText="Hello" />
</Binding.ValidationRules>
</TextBox.Text>
</TextBox>Deja View - the feeling that you've seen this post before.