我想允许通过键盘检查单选按钮,特别是通过数字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;
}
}
......但是知道这是kludgy'verbose。对一部分密钥采取行动并忽略其余部分的更好方法是什么?