Microsoft Developer Network > Domovská stránka fór > Windows Presentation Foundation (WPF) > How to simulate a button click within the code.
Odeslat dotazOdeslat dotaz
 

OdpovědětHow to simulate a button click within the code.

  • 3. července 2009 2:50Victor Sirghii Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    How do I simulate the press of a WPF button from within the code? I would like to call a method from within my code and have a particular button on a WPF form look (visually behave) like it had been pressed and released as this button does when a user actually clicks it with the mouse.
    Why? I have a form with several buttons. When a user presses the appropriate key on the keyboard, I would like to provide visual feedback that the key which had been pressed was equivalent to a particular button.
    Such kind solutions like:  

    1.  button1.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

    2.  ButtonAutomationPeer peer = new ButtonAutomationPeer(button1);

    AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement(button1);                                

    IInvokeProvider invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;

    invokeProv.Invoke();

    does not work for me Both of them call/raise/simulate Click event of button1, but there is no visual feedback. The button on the screen does not look “pressed” and then “released”. May be because the RaiseEvent as well as Automation work too fast, may be because of something else, I don’t know, but you can’t observe the reaction of button1 in cases above.

Odpovědi

  • 20. července 2009 21:31Kenneth Haugland Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
  • 14. srpna 2009 8:19Jim Zhou - MSFTModerátorUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    Hi,
    -->does not work for me Both of them call/raise/simulate Click event of button1, but there is no visual feedback. The button on the screen does not look “pressed” and then “released”.

    I also notice that we can not achieve the visual button click appearance via AutomationPeer logic. Alternatively, we can contron the visual of the button and make it looks like being pressed. when may notice that when you click a button, the bounds of button is changed, so we can change the visual feedback via the ScaleTransform property. You can have a look at the following code.
    In addition, we can also set the FocusVIsualStyle to get a visual dashed line around the button to simulate a visual feedback more exactly.

    Code segment:

     bool flag = false;

            private void OnRaiseEvent(object sender, RoutedEventArgs e)

            {

                if (flag == false)

                {

                    button1.RenderTransform = new ScaleTransform(0.7, 0.7);

                    //you can also control the scale center by setting CenterX/CenterY property

                    button1.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

                    flag = true;

                }

                else

                {

                    button1.RenderTransform = new ScaleTransform(1, 1);

                    flag = false;

                }

            }

            private void OnClick(object sender, RoutedEventArgs e)

            {

                //on the click event

            }


    Thanks.

    Jim Zhou -MSFT

Všechny reakce

  • 20. července 2009 18:35Rath Shetty[MSFT]MSFTUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     

    your UIA soultion should work ( you can confirm by checking whether OnClick handler getting called) else try the below one.

    System.Windows.Controls.Button button = new Button();
    button.RaiseEvent(new System.Windows.RoutedEventArgs(System.Windows.Controls.Button.ClickEvent, button));

     

  • 20. července 2009 21:31Kenneth Haugland Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
  • 14. srpna 2009 8:19Jim Zhou - MSFTModerátorUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    Hi,
    -->does not work for me Both of them call/raise/simulate Click event of button1, but there is no visual feedback. The button on the screen does not look “pressed” and then “released”.

    I also notice that we can not achieve the visual button click appearance via AutomationPeer logic. Alternatively, we can contron the visual of the button and make it looks like being pressed. when may notice that when you click a button, the bounds of button is changed, so we can change the visual feedback via the ScaleTransform property. You can have a look at the following code.
    In addition, we can also set the FocusVIsualStyle to get a visual dashed line around the button to simulate a visual feedback more exactly.

    Code segment:

     bool flag = false;

            private void OnRaiseEvent(object sender, RoutedEventArgs e)

            {

                if (flag == false)

                {

                    button1.RenderTransform = new ScaleTransform(0.7, 0.7);

                    //you can also control the scale center by setting CenterX/CenterY property

                    button1.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

                    flag = true;

                }

                else

                {

                    button1.RenderTransform = new ScaleTransform(1, 1);

                    flag = false;

                }

            }

            private void OnClick(object sender, RoutedEventArgs e)

            {

                //on the click event

            }


    Thanks.

    Jim Zhou -MSFT
  • 14. srpna 2009 9:47DanielRose Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Obsahuje kód

    My idea was to use the MouseLeftButtonDown and MouseLeftButtonUp events.


    System.Windows.Controls.Button button = new System.Windows.Controls.Button();
    button.RaiseEvent(new System.Windows.RoutedEventArgs(System.Windows.Controls.Button.MouseLeftButtonDownEvent, button));
    System.Threading.Thread.Sleep(300);
    button.RaiseEvent(new System.Windows.RoutedEventArgs(System.Windows.Controls.Button.MouseLeftButtonUpEvent, button));
    

    Unfortunately, I seem to hit a framework bug(?): The button tries to convert the RoutedEventArgs into MouseButtonEventArgs. This fails.