Formular una preguntaFormular una pregunta
 

RespondidaHow to simulate a button click within the code.

  • viernes, 03 de julio de 2009 2:50Victor Sirghii Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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.

Respuestas

  • lunes, 20 de julio de 2009 21:31Kenneth Haugland Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
  • viernes, 14 de agosto de 2009 8:19Jim Zhou - MSFTModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    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

Todas las respuestas

  • lunes, 20 de julio de 2009 18:35Rath Shetty[MSFT]MSFTMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

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

     

  • lunes, 20 de julio de 2009 21:31Kenneth Haugland Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
  • viernes, 14 de agosto de 2009 8:19Jim Zhou - MSFTModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    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
  • viernes, 14 de agosto de 2009 9:47DanielRose Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Tiene código

    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.