简体   繁体   中英

How do I make xctk:IntegerUpDown accept digits only?

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?

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM