我有一个使用WPF的c#程序。我知道当你按一个键时,它会点击一个按钮,然后禁用该按钮。我的问题是,一旦我决定点击一个按钮,keyDown事件就不再被执行了。我错过了什么吗?
namespace FindTheCat
{
public partial class MainWindow : Window
{
private Treasure treas;
public MainWindow()
{
InitializeComponent();
}
private void checkForKitty(object sender, RoutedEventArgs e)
{
ToggleButton button = (ToggleButton)e.OriginalSource;
if(button != null)
{
button.IsEnabled = false;
int loc = getLocation((String)button.Content);
if (loc > 0)
{
isFound(loc);
}
}
}
private void checkKey(object sender, KeyEventArgs e)
{
ToggleButton item = (ToggleButton)GridRoot.FindName(e.Key.ToString());
if (item != null)
{
item.IsEnabled = false;
int loc = getLocation(e.Key.ToString());
if (loc > 0)
{
isFound(loc);
}
}
MessageBox.Show("Key Pressed");
}
private void isFound(int loc)
{
Boolean found = treas.isFound(loc);
if (found)
{
MessageBox.Show("YAY! You found the cat!");
Reset();
}
else
{
MessageBox.Show("Boo! You did not find the cat!");
}
}
private int getLocation(String letter)
{
switch (letter)
{
case "A":
return 1;
case "B":
return 2;
case "C":
return 3;
case "D":
return 4;
case "E":
return 5;
case "F":
return 6;
case "G":
return 7;
case "H":
return 8;
case "I":
return 9;
case "J":
return 10;
case "K":
return 11;
case "L":
return 12;
case "M":
return 13;
case "N":
return 14;
case "O":
return 15;
case "P":
return 16;
case "Q":
return 17;
case "R":
return 18;
case "S":
return 19;
case "T":
return 20;
case "U":
return 21;
case "V":
return 22;
case "W":
return 23;
case "X":
return 24;
case "Y":
return 25;
default:
return 0;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
treas = new Treasure();
}
private void Reset()
{
treas.Reset();
ResetButton("A");
ResetButton("B");
ResetButton("C");
ResetButton("D");
ResetButton("E");
ResetButton("F");
ResetButton("G");
ResetButton("H");
ResetButton("I");
ResetButton("J");
ResetButton("K");
ResetButton("L");
ResetButton("M");
ResetButton("N");
ResetButton("O");
ResetButton("P");
ResetButton("Q");
ResetButton("R");
ResetButton("S");
ResetButton("T");
ResetButton("U");
ResetButton("V");
ResetButton("W");
ResetButton("X");
ResetButton("Y");
}
private void ResetButton(String name)
{
ToggleButton item = (ToggleButton)GridRoot.FindName(name);
if (item != null)
{
item.IsEnabled = true;
item.IsChecked = false;
}
}
}
}
答案 0 :(得分:1)
禁用的控件无法使用键盘焦点。所以它不会得到任何关键事件。