[英]How do I make xctk:IntegerUpDown accept digits only?
我正在嘗試使xctk:IntegerUpDown
僅接受數字。 老實說,我很驚訝為什么數字控件會接受非數字。 如果有人知道其實際原因,請告訴我。
因此,我嘗試了以下操作:將FormatString
為N
,將ParsingNumberStyle
為Integer
但是它仍然接受非數字字符,例如字母。 我該如何解決?
您可以使用PreviewKeyDown
事件。 我為TextBox
添加了一個示例,該示例也應適用於您的NumericUpDown
。
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.