单击按钮控件并处理事件后,按钮控件将一直闪烁

这与WPF和C#有关。 我的程序中有几个按钮,当它们被点击时,即使在处理事件后它们也会继续闪烁。 例如,我所拥有的按钮应该根据用户输入打开一个新窗口。 如果用户的输入不正确,MessageBox会出现这样的说法。 关闭MessageBox后,按钮开始闪烁。 如果用户输入正确,则会打开新窗口。 一旦我从新窗口点击进入旧窗口,按钮开始闪烁; 如果我关闭新窗口,按钮开始闪烁。

我尝试在我的代码中使用this.Focus()来关注此按钮,以便将焦点放在主窗口上。 我尝试使用e.Handled = true,但似乎没有什么能阻止它。 我不想通过将该属性设置为false来使按钮不是Focusuable,因为我希望我的程序可以访问。

有什么想法正在发生什么?

这是我的按钮的XAML代码:

按钮的C#代码(这没有this.Focus()因为它对我不起作用):

 private void btnSearch_Click(object sender, RoutedEventArgs e) { if (!String.IsNullOrEmpty(txtNumber.Text.ToString()) && txtNumber.Text.ToString().Length >= 10) { if (QueryWindow == null) { QueryWindow = new DatabaseQueryWindow(); QueryWindow.Show(); QueryWindow.Closed += new EventHandler(QueryWindow_Closed); } else if (QueryWindow != null && !QueryWindow.IsActive) { QueryWindow.Activate(); } QueryDB(); } else { MessageBox.Show("Please enter a valid # (Format: YYYYmm####)"); } } void QueryWindow_Closed(object sender, EventArgs e) { QueryWindow = null; } private void Button_MouseEnter(object sender, MouseEventArgs e) { Button b = sender as Button; if (b != null) { b.Foreground = Brushes.Black; } } private void Button_MouseLeave(object sender, MouseEventArgs e) { Button b = sender as Button; if (b != null) { b.Foreground = Brushes.White; } } 

对于任何有兴趣如何在不禁用按钮的情况下摆脱这种行为的人:

执行操作后,只需将焦点重定向到另一个控件,例如按钮旁边的文本框:txtBox.Focus();

为我工作。 我无法找到另一种简单的方法。