.NET Framework Developer Center >
.NET Development Forums
>
Windows Presentation Foundation (WPF)
>
How "press" a WPF button from within code?
How "press" a WPF button from within code?
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
- 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
- You can use the RaiseEvent method:
button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));- Marked As Answer byJim Zhou - MSFTModeratorWednesday, June 10, 2009 12:25 PM
- Unmarked As Answer bytsrCharles Wednesday, June 10, 2009 11:53 PM
- 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."
- 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- Unmarked As Answer bytsrCharles Wednesday, June 10, 2009 11:53 PM
- Marked As Answer byJim Zhou - MSFTModeratorWednesday, June 10, 2009 12:25 PM
- 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 ...
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- 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 - 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
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- 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
- 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


