单击并处理事件后,按钮控件将继续闪烁 [英] Button control keeps flashing after it has been clicked and handled the event

查看:47
本文介绍了单击并处理事件后,按钮控件将继续闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is related to WPF and 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. Once I close the MessageBox, button begins flashing. 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.

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

I tried using this.Focus() all over my code that concerns this button to get the focus on the main window. I tried using e.Handled = true, but nothing seems to stop it. I don't want to make the button not Focusuable by setting that property to false, because I want my program to be accessible.

有什么想法吗?

这是该按钮的XAML代码:

Here's my XAML code for the button:

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

按钮的C#代码(此按钮没有this.Focus(),因为它不适用于我):

C# code for the button (this doesn't have this.Focus() because it didn't work for me):

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:

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

After an action is executed, just redirect focus to another control, such as a textbox that's next to a button: txtBox.Focus();

为我工作.我找不到其他简单的方法.

Worked for me. I wasn't able to find another simple way.

这篇关于单击并处理事件后,按钮控件将继续闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆