正如标题所言。 我看过从 TextBox 继承,但唯一合理的覆盖是“OnKeyDown”,但这只是从 Key 枚举中给了我一个键(无法使用 Char.IsNumeric())。
最佳答案
我接受了 Nidhal 建议的答案并对其进行了一些编辑以处理数字上方字符的移位情况(即 !@#$%^&*()),因为该解决方案仍将允许文本框中的这些字符。
private void NumClient_KeyDown(object sender, KeyEventArgs e)
{
// Handle Shift case
if (Keyboard.Modifiers == ModifierKeys.Shift)
{
e.Handled = true;
}
// Handle all other cases
if (!e.Handled && (e.Key < Key.D0 || e.Key > Key.D9))
{
if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
{
if (e.Key != Key.Back)
{
e.Handled = true;
}
}
}
}
关于c# - 如何在 Silverlight 中创建数字文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/268207/