簡體   English   中英

單擊按鈕控件並處理事件后,按鈕控件將一直閃爍

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

這與WPF和C#有關。 我的程序中有幾個按鈕,當它們被點擊時,即使在處理事件后它們也會繼續閃爍。 例如,我所擁有的按鈕應該根據用戶輸入打開一個新窗口。 如果用戶的輸入不正確,MessageBox會出現這樣的說法。 關閉MessageBox后,按鈕開始閃爍。 如果用戶輸入正確,則會打開新窗口。 一旦我從新窗口點擊進入舊窗口,按鈕開始閃爍; 如果我關閉新窗口,按鈕開始閃爍。

我嘗試在我的代碼中使用this.Focus()來關注此按鈕,以便將焦點放在主窗口上。 我嘗試使用e.Handled = true,但似乎沒有什么能阻止它。 我不想通過將該屬性設置為false來使按鈕不是Focusuable,因為我希望我的程序可以訪問。

有什么想法發生了什么?

這是按鈕的XAML代碼:

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

按鈕的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();

為我工作。 我無法找到另一種簡單的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM