按钮控件在单击并处理事件后保持闪烁



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

我试过用这个。Focus() 遍布我的代码,涉及此按钮以获得主窗口的焦点。 我尝试使用 e.Handle = true,但似乎没有什么能阻止它。 我不想通过将按钮属性设置为 false 来使按钮不可聚焦,因为我希望我的程序可以访问。

知道发生了什么吗?

下面是按钮的 XAML 代码:

<Button x:Name="btnSearch" Content="Search" Background="#4a89be" Foreground="White"
        MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave"
        Click="btnSearch_Click" />

按钮的 C# 代码(这没有这个。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();

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

最新更新