我想允许通过键盘检查单选按钮,特别是通过数字 1、2、3。我有这个工作的代码:
// "a WPF form hears what it wants to hear and disregards the rest" - paraphrased from "The Boxer" by Simon & Garfunkel
private void Window_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs keArgs)
{
var keyPressed = keArgs.Key.ToString();
if ((keyPressed.Equals("D1")) || (keyPressed.Equals("NumPad1")))
{
rdbtnCandidateVal1.IsChecked = true;
}
else if ((keyPressed.Equals("D2")) || (keyPressed.Equals("NumPad2")))
{
rdbtnCandidateVal2.IsChecked = true;
}
else if ((keyPressed.Equals("D3")) || (keyPressed.Equals("NumPad3")))
{
rdbtnCandidateVal3.IsChecked = true;
}
}
...但要知道这是很冗长的。对一部分键采取行动而忽略其余部分的更好方法是什么?