Microsoft Developer Network > Forenhomepage > Windows Presentation Foundation (WPF) > How to simulate a button click within the code.
Stellen Sie eine FrageStellen Sie eine Frage
 

BeantwortetHow to simulate a button click within the code.

  • Freitag, 3. Juli 2009 02:50Victor Sirghii TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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.

Antworten

  • Montag, 20. Juli 2009 21:31Kenneth Haugland TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
  • Freitag, 14. August 2009 08:19Jim Zhou - MSFTModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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

Alle Antworten

  • Montag, 20. Juli 2009 18:35Rath Shetty[MSFT]MSFTTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     

    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));

     

  • Montag, 20. Juli 2009 21:31Kenneth Haugland TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
  • Freitag, 14. August 2009 08:19Jim Zhou - MSFTModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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
  • Freitag, 14. August 2009 09:47DanielRose TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Enthält Code

    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.