简体   繁体   English

如何使xctk:IntegerUpDown仅接受数字?

[英]How do I make xctk:IntegerUpDown accept digits only?

I'm trying to make a xctk:IntegerUpDown accepts only digits. 我正在尝试使xctk:IntegerUpDown仅接受数字。 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. 因此,我尝试了以下操作:将FormatStringN ,将ParsingNumberStyleInteger但是它仍然接受非数字字符,例如字母。 How do I fix this? 我该如何解决?

You can use the PreviewKeyDown event. 您可以使用PreviewKeyDown事件。 I add an example with a TextBox that should work for your NumericUpDown as well. 我为TextBox添加了一个示例,该示例也应适用于您的NumericUpDown

xaml: xaml:

<TextBox PreviewKeyDown="OnPreviewKeyDown" />

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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