小智 7
您可以处理PreviewTextInput事件:
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
// Filter out non-digit text input
foreach (char c in e.Text)
if (!Char.IsDigit(c))
{
e.Handled = true;
break;
}
}
Run Code Online (Sandbox Code Playgroud)