XTab's Blog

Ged Mead's Blog at vbCity

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

OctNovember 2009Dec
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

Topics

Ramblings

VB.NET

  PerformClick is one of the many casualties of a move from Windows Forms to WPF. Of course, it's probably not something you would need to use often, but just in case you do then there is a way.

  It's not a very intuitive way and - had it not been that I spotted the C# version of this trick in Adam Nathan's WPF book - I wouldn't be sat here writing this now. I'd still be searching endlessly to try and find the answer.

   In order to achieve this in VB, you need to do the following:

  Add a Reference to UIAutomationProvider in your Project.

  Add an Imports statement:

Imports System.Windows.Automation.Peers

  Add another Imports statement:
Imports System.Windows.Automation.Provider

  Use the following code snippet:
Dim buttonPeer As New ButtonAutomationPeer(Button1)
Dim invokeProv As IInvokeProvider = TryCast(buttonPeer.GetPattern(PatternInterface.Invoke), IInvokeProvider)
invokeProv.Invoke()


   The above code to be placed wherever you want to simulate the Button's Click. (Change the Button name as needed).

  It did occur to me that this task might be a good candidate for a custom Command, using WPF's quite versatile Command abilities. However, I haven't got past the "wonder if that's a good idea?" stage. And to be honest, probably won't any time soon.

  Whether you feel that it's necessary to go to the trouble of the Automation or the Command approach is of course entirely up to you. I have to admit to being tempted simply to move the action code out of the Button Click event and into a separate method that can be called from both the Button Click and other events.  But then, I've never been much or a purist about these things.

posted on Saturday, November 15, 2008 3:59 PM

Feedback

# re: WPF Missing In Action - The Button PerformClick Method 12/3/2008 9:36 AM Kannan Perumal
C#:

(new ButtonAutomationPeer(button_AddNds).GetPattern(PatternInterface.Invoke) as IInvokeProvider).Invoke();

# re: WPF Missing In Action - The Button PerformClick Method 12/12/2008 8:05 PM Ged
Thanks, Kannan!

# WPF Commands and PerformClick 1/18/2009 2:04 AM XTab's Blog


# WPF Commands and PerformClick 1/18/2009 2:18 AM XTab's Blog
Some weeks ago I wrote a blog item about how to get the functionality of PerformClick in WPF. I remember

# re: WPF Missing In Action - The Button PerformClick Method 5/18/2009 2:04 PM Richard
If you're not using commands, you can either call the event handler directly, or fire the click event manually:

button_AddNds.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent, button_AddNds))

# Great post 10/24/2009 3:11 AM MarkRight
Great post you got here. It would be great to read a bit more concerning that topic.

Post Feedback

Title:
Name:
Url:
Comments: 
Protected by Clearscreen.SharpHIPEnter the code you see: