Reputation: 16724
I'm trying to make a xctk:IntegerUpDown
accepts only digits. To be honest, I was surprised why a numeric control would accepts non-digits. If anyone know the actual reason for this, please tell me.
So I've tried to:
FormatString
to N
and ParsingNumberStyle
to Integer
but it still accepts non-digit chars such as letters. How do I fix this?
Upvotes: 0
Views: 599
Reputation: 1649
You can use the PreviewKeyDown
event. I add an example with a TextBox
that should work for your NumericUpDown
as well.
xaml:
<TextBox PreviewKeyDown="OnPreviewKeyDown" />
cs:
public void OnPreviewKeyDown(object sender, KeyEventArgs args)
{
switch (args.Key) {
case Key.D0: case Key.NumPad0:
case Key.D1: case Key.NumPad1:
case Key.D2: case Key.NumPad2:
case Key.D3: case Key.NumPad3:
case Key.D4: case Key.NumPad4:
case Key.D5: case Key.NumPad5:
case Key.D6: case Key.NumPad6:
case Key.D7: case Key.NumPad7:
case Key.D8: case Key.NumPad8:
case Key.D9: case Key.NumPad9:
args.Handled = false;
break;
default:
args.Handled = true;
break;
}
}
Upvotes: 2