Answer the question
In order to leave comments, you need to log in
Validation in wpf?
How to make it so that you can't enter numbers in a textbox
Answer the question
In order to leave comments, you need to log in
Handle the TextChanged event like so:
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
var index = textBox.CaretIndex;
textBox.Text = Regex.Replace(textBox.Text, @"\d", "");
textBox.CaretIndex = index;
}
public class OnlyTextValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
bool noNumbers = Regex.Matches("^([^0-9]*)$",value.ToString()).Count > 0;
return new ValidationResult(noNumbers, "Value contains numbers");
}
}
<Window.Resources>
<local:OnlyTextValidationRule x:Key="NoNumberValidate"/>
</Window.Resources>
<TextBox Text={Binding Value, ValidationRules={StaticResource NoNumberValidate}}/>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question