简体   繁体   English

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

[英]Button control keeps flashing after it has been clicked and handled the event

This is related to WPF and C#. 这与WPF和C#有关。 I have several buttons in my program and when they're being clicked, they'll keep on flashing even after handling an event. 我的程序中有几个按钮,当它们被点击时,即使在处理事件后它们也会继续闪烁。 For example, of the buttons that I have is supposed to open a new window based on the user input. 例如,我所拥有的按钮应该根据用户输入打开一个新窗口。 If the user's input is incorrect and MessageBox will come up saying so. 如果用户的输入不正确,MessageBox会出现这样的说法。 Once I close the MessageBox, button begins flashing. 关闭MessageBox后,按钮开始闪烁。 If the user input is correct, the new window will open. 如果用户输入正确,则会打开新窗口。 Once I click away from the new window into the old window, button begins to flash; 一旦我从新窗口点击进入旧窗口,按钮开始闪烁; if I close the new window, button begins to flash. 如果我关闭新窗口,按钮开始闪烁。

I tried using this.Focus() all over my code that concerns this button to get the focus on the main window. 我尝试在我的代码中使用this.Focus()来关注此按钮,以便将焦点放在主窗口上。 I tried using e.Handled = true, but nothing seems to stop it. 我尝试使用e.Handled = true,但似乎没有什么能阻止它。 I don't want to make the button not Focusuable by setting that property to false, because I want my program to be accessible. 我不想通过将该属性设置为false来使按钮不是Focusuable,因为我希望我的程序可以访问。

Any ideas what's going on? 有什么想法发生了什么?

Here's my XAML code for the button: 这是按钮的XAML代码:

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

C# code for the button (this doesn't have this.Focus() because it didn't work for me): 按钮的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;
    }
}

For anyone interested in how to get rid of this behavior without disabling focus on buttons: 对于任何有兴趣如何在不禁用按钮的情况下摆脱这种行为的人:

After an action is executed, just redirect focus to another control, such as a textbox that's next to a button: txtBox.Focus(); 执行操作后,只需将焦点重定向到另一个控件,例如按钮旁边的文本框:txtBox.Focus();

Worked for me. 为我工作。 I wasn't able to find another simple way. 我无法找到另一种简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM