If I can assume that the input goes in
TextBox
, you can always filter out unwanted characters.
You need to use the event
PreviewTextInput
, which you can cancel. For example
using System.Windows;
static bool IsNumericOnly(string value) {
foreach(char digit in value)
if (!char.IsDigit(digit))
return false;
return true;
}
textBox.PreviewTextInput += (sender, eventArgs) => {
eventArgs.Handled = !IsNumericOnly(eventArgs.Text);
};
—SA