Ask a questionAsk a question
 

AnswerHow "press" a WPF button from within code?

  • Thursday, June 04, 2009 3:30 PMtsrCharles Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    How do I simulate the press of a WPF button from within code? I would like to call a method from within my code and have a particular button on a WPF form look like (visually behave like) it had been pressed and released (and presumably also fire its click event). I seem to recall doing this years ago with win forms and VB. Thanks!

    Why? I have a form with several buttons and some keyboard shortcuts for those buttons. When a user presses the appropriate key, I would like to provide visual feedback that the key that was pressed was equivalent to a particular button.

Answers

  • Sunday, July 19, 2009 4:05 AMmcm_ham Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You can set ReadOnly properties using reflection (although not a recommended programming practice):

    typeof(Button).GetMethod("set_IsPressed", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(BtnInstance , new object[] { true });

    Then you can use your timer to set it unpressed again.
    • Edited bymcm_ham Sunday, July 19, 2009 4:20 AM
    • Marked As Answer bytsrCharles Monday, July 20, 2009 1:27 AM
    • Edited bymcm_ham Sunday, July 19, 2009 4:08 AM
    •  

All Replies

  • Thursday, June 04, 2009 4:01 PMMariano O. Rodriguez Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    You can use the RaiseEvent method:

    button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
    
  • Thursday, June 04, 2009 4:11 PMtsrCharles Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks. Halfway there. That calls the button's Click event but does not provide the visual feedback. The button on the screen does not "press."
  • Thursday, June 04, 2009 6:29 PMKrishnaBhargava Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    http://joshsmithonwpf.wordpress.com/2007/03/09/how-to-programmatically-click-a-button/

    Try the AutomationPeer as described in Josh' post.
    Software Engineer 1, My Blog
  • Thursday, June 04, 2009 7:11 PMtsrCharles Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Krishna - Thanks, but that seems to be a more complicated way of doing the same thing as .RaiseEvent (as one of the replies there indicates). Yes, it calls the Click event, but there is no "press" in the visual representation of the button on the screen.

    I fired up my old VB6 and I can't find it a method there either. VB6 command buttons don't have very many methods, so that was a pretty easy search.

    So, still asking ...
  • Sunday, June 28, 2009 2:58 PMKenneth Haugland Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     


    I think the source code from the book Application = Code + Markup in the Vb.net translated code gives an excample how to do this... I caant find the vb.net code thou...


    Kenneth
  • Sunday, June 28, 2009 6:41 PMtsrCharles Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Kenneth, thanks (again!). I think you're right. I can see enough of the book on Amazon to see what he is doing. (The example is actually in C#.) It involves a Decorator for the button and overridding the Button class to make IsPressed read/write rather than read-only (I think). I started playing with it a little bit and I'm hitting a problem with the dependency property IsPressedProperty which is apparently supposed to be in System.Windows.Controls.Primitives but apparently is not (or I'm missing something, more likely).

    I think I may put this on hold as I currently have two other parts of the application "under construction" and I'm relutctant to dig into this too deeply without tying up the other loose ends first.

    I've got an 80% solution implemented: I re-draw the button's border with a black pen and start a 100 millisecond timer to draw it back as it was. It's a fairly good simulation of a depressed button, but not perfect. (Close inspection of the pixels reveals that a standard button depression involves a 1-pixel gray border immediately inside a 1-pixel black border.)
    Charles
  • Sunday, June 28, 2009 6:48 PMKenneth Haugland Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi again...

    This link is for code in VB

    http://code.msdn.microsoft.com/petzoldsamplevb/Release/ProjectReleases.aspx?ReleaseId=88

    But on second thouth, i dont think the code shows how to emulet a button pressed like you wanted... You can, I guess, emulate a mouse right button push... But that will be over my head....


    Let me know what you find out..... I want the solution to this as well:)


    Kenneth
  • Sunday, June 28, 2009 7:22 PMKenneth Haugland Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    See this post...

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/ad5beda6-3ba5-4c50-8199-faf48505ad13

    You could solve it that way....


    Kenneth
  • Sunday, July 19, 2009 4:05 AMmcm_ham Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You can set ReadOnly properties using reflection (although not a recommended programming practice):

    typeof(Button).GetMethod("set_IsPressed", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(BtnInstance , new object[] { true });

    Then you can use your timer to set it unpressed again.
    • Edited bymcm_ham Sunday, July 19, 2009 4:20 AM
    • Marked As Answer bytsrCharles Monday, July 20, 2009 1:27 AM
    • Edited bymcm_ham Sunday, July 19, 2009 4:08 AM
    •  
  • Monday, July 20, 2009 1:26 AMtsrCharles Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks. Good to know.

    I've built my own button, a custom control consisting of a grid containing a rectangle and a label. I have a property IsDepressed which is available to one and all.
    Charles