XTab's Blog

Ged Mead's Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

DecJanuary 2009Feb
SMTWTFS
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

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!

Post Feedback

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