按下默认键时显示按下按钮效果

时间:2012-04-27 04:54:08

标签: c# wpf visual-studio-2010 xaml

当我按下默认键(输入)时,我希望我的按钮具有按下并释放效果(模拟鼠标按下)。

我试过这样做

   private void OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                VisualStateManager.GoToState(btnLogin, "Pressed", true);
                System.Threading.Thread.Sleep(300);
                VisualStateManager.GoToState(btnLogin, "Normal", true);
                btnLogin_Click(sender, e);

            }
        }

无法让它发挥作用。

2 个答案:

答案 0 :(得分:0)

你做了按钮Property IsDefault = True

答案 1 :(得分:0)

尝试以下方法 “

private void OnKeyDownHandler(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            VisualStateManager.GoToState(btnLogin, "Pressed", true);
            this.Refresh();
            System.Threading.Thread.Sleep(300);
            VisualStateManager.GoToState(btnLogin, "Normal", true);
            this.Refresh();
            btnLogin_Click(sender, e);
        }
    }