Automation vs RaiseEvent<p>I recently blogged about how to programmatically click a Button in WPF (<a title="http://joshsmithonwpf.wordpress.com/2007/03/09/how-to-programmatically-click-a-button" href="http://joshsmithonwpf.wordpress.com/2007/03/09/how-to-programmatically-click-a-button">http://joshsmithonwpf.wordpress.com/2007/03/09/how-to-programmatically-click-a-button</a>).  I used the automation API to click the button:</p> <p>ButtonAutomationPeer peer =<br>  new ButtonAutomationPeer( someButton );</p> <p>IInvokeProvider invokeProv =<br>  peer.GetPattern( PatternInterface.Invoke )<br>  as IInvokeProvider;</p> <p>invokeProv.Invoke();</p> <p>Someone left a comment mentioning that you can use the RaiseEvent method to achieve the same result:</p> <p>someButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));</p> <p>My question is, which is better?  What's the difference between using these two approaches?  Which does the WPF team at MSFT recommend?</p> <p>Thanks for any insights.</p>© 2009 Microsoft Corporation. All rights reserved.Tue, 07 Jul 2009 22:11:58 Z1ba3696e-6ca2-48de-80df-31477c9504cehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#1ba3696e-6ca2-48de-80df-31477c9504cehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#1ba3696e-6ca2-48de-80df-31477c9504ceJosh Smithhttp://social.msdn.microsoft.com/Profile/en-US/?user=Josh%20SmithAutomation vs RaiseEvent<p>I recently blogged about how to programmatically click a Button in WPF (<a title="http://joshsmithonwpf.wordpress.com/2007/03/09/how-to-programmatically-click-a-button" href="http://joshsmithonwpf.wordpress.com/2007/03/09/how-to-programmatically-click-a-button">http://joshsmithonwpf.wordpress.com/2007/03/09/how-to-programmatically-click-a-button</a>).  I used the automation API to click the button:</p> <p>ButtonAutomationPeer peer =<br>  new ButtonAutomationPeer( someButton );</p> <p>IInvokeProvider invokeProv =<br>  peer.GetPattern( PatternInterface.Invoke )<br>  as IInvokeProvider;</p> <p>invokeProv.Invoke();</p> <p>Someone left a comment mentioning that you can use the RaiseEvent method to achieve the same result:</p> <p>someButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));</p> <p>My question is, which is better?  What's the difference between using these two approaches?  Which does the WPF team at MSFT recommend?</p> <p>Thanks for any insights.</p>Sat, 10 Mar 2007 17:30:40 Z2007-03-12T00:41:22Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#c658e3a8-b644-42d4-b7a4-c8ca97312923http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#c658e3a8-b644-42d4-b7a4-c8ca97312923Kane-http://social.msdn.microsoft.com/Profile/en-US/?user=Kane-Automation vs RaiseEventI always thought that the difference was that the UI automation approach was for accessing UI elements that aren't inherently part of your app, while the raiseEvent approach can only be used on WPF objects.<br><br>I got the impression that UI automation was introduced to help out with testing in the form of script / batch testing, rather than to be used as the regular method for initiating a <span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody" class="txt4">programatically </span></span>initiated event.<br><br>Edit: Just to clarify that raiseEvent can be used on any WPF object rather than just buttons.<br><br>Sun, 11 Mar 2007 01:08:16 Z2007-03-11T01:08:16Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#075d4746-76e5-4111-958f-f6227af20172http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#075d4746-76e5-4111-958f-f6227af20172Neil Mosafihttp://social.msdn.microsoft.com/Profile/en-US/?user=Neil%20MosafiAutomation vs RaiseEventI would probably go with the Automation approach as manually raising the event does not properly simulate what happens if the button is clicked with the mouse. <br>E.g what if the button binds to a command or is disabled?<br>Sun, 11 Mar 2007 18:05:36 Z2007-03-12T00:41:22Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#bf2216e8-7ab0-4f2a-be41-faef430e74e5http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#bf2216e8-7ab0-4f2a-be41-faef430e74e5Josh Smithhttp://social.msdn.microsoft.com/Profile/en-US/?user=Josh%20SmithAutomation vs RaiseEvent<p>Neil,</p> <p>Thanks, that's a good point.</p> <p>Josh</p>Mon, 12 Mar 2007 00:41:56 Z2007-03-12T00:41:56Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#e509cdc3-dfd0-482f-ab70-abfa358731ddhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#e509cdc3-dfd0-482f-ab70-abfa358731ddsfedorovhttp://social.msdn.microsoft.com/Profile/en-US/?user=sfedorovAutomation vs RaiseEventRaising event has a flavor of invoking object's private methods through reflection. Certainly violates contract and potentially makes object's state inconsistent - not to mention that such an invocation could be simply meaningless and/or misleading.<br><br><br>Mon, 12 Mar 2007 15:15:49 Z2007-03-12T15:15:49Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#e928e5ce-d6d4-421f-8e46-b8b864f02323http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#e928e5ce-d6d4-421f-8e46-b8b864f02323beckycataniahttp://social.msdn.microsoft.com/Profile/en-US/?user=beckycataniaAutomation vs RaiseEvent<p>I have tried using the Automation example provided by Josh, RaiseEvent and also just calling the event handler directly.  In each of these approaches, the button does not &quot;look&quot; clicked on the screen - meaning the mouse over outline and background color do not changed as they do when a user actually clicks it with the mouse.  I want to initiate a button click programmatically and also have the button &quot;look clicked&quot; for the user.  How can I do that?  I really appreciate your help.  Here is some of my code (all of which raises the appropriate event but does not change the appearance of my button).  My button name is btnA.  </p> <p align=left> </p> <p><u>Animation example code</u>:</p> <p> </p><font size=2> <p></font><font color="#0000ff" size=2>Dim</font><font size=2> peer </font><font color="#0000ff" size=2>As</font><font size=2> </font><font color="#0000ff" size=2>New</font><font size=2> ButtonAutomationPeer(btnA)</p> <p></font><font color="#0000ff" size=2>Dim</font><font size=2> invokeProv </font><font color="#0000ff" size=2>As</font><font size=2> IInvokeProvider = peer.GetPattern(PatternInterface.Invoke)</p> <p>invokeProv.Invoke()</p></font> <p align=left><font face=Arial size=2></font> </p> <p align=left><u>RaiseEvent code</u>:</p> <p align=left> </p><font size=2> <p>btnA.RaiseEvent(</font><font color="#0000ff" size=2>New</font><font size=2> System.Windows.RoutedEventArgs(Button.ClickEvent))</p></font> <p align=left> </p> <p align=left> </p> <p align=left>P.S. I also tried just programmatically setting the button border and background to appear clicked but since there is no window.refresh anymore the window does not repaint to achieve the simulated click.</p> <p align=left> </p>Any ideas?  Thanks!Fri, 14 Mar 2008 14:40:02 Z2008-03-14T14:40:02Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#e19b3c25-9462-4c63-bab5-66d870db7b95http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#e19b3c25-9462-4c63-bab5-66d870db7b95Himanshu Arorahttp://social.msdn.microsoft.com/Profile/en-US/?user=Himanshu%20AroraAutomation vs RaiseEventWell you should really not expect that &quot;clicked&quot; behaviour which is due to your mouse hover/down/up and totally depends on the user clicking the button. And the RaiseEvent as well as Automation are just for fraction of second so I am not sure if there may be such behaviour which is quite difficult to observe. <br><br>Another thing you can do is to animate the button when you raise the click event.<br><br>Thanks<br><br>--<br>Himanshu<br>Fri, 28 Mar 2008 10:03:55 Z2008-03-28T10:03:55Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#73325de5-f67c-405c-8722-27248f3ba01ehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#73325de5-f67c-405c-8722-27248f3ba01ebeckycataniahttp://social.msdn.microsoft.com/Profile/en-US/?user=beckycataniaAutomation vs RaiseEventThank you, Himanshu.  I thought about using Animation.  I'll try that.  Thanks! <p align=left><font face=Arial size=2></font> </p>Fri, 28 Mar 2008 18:01:44 Z2008-03-28T18:01:44Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#e7a9f926-42f3-4c49-b118-0801959f1757http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#e7a9f926-42f3-4c49-b118-0801959f1757Andrey Kozyrevhttp://social.msdn.microsoft.com/Profile/en-US/?user=Andrey%20KozyrevAutomation vs RaiseEventMy 2 cents: Automation is a better way because there can be some other logic in between the call and an event raise. Consider simulating the click on the menu item of the context menu. Click will close the menu, raise will not.Fri, 20 Mar 2009 00:44:44 Z2009-03-20T00:44:44Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#f0549536-f0db-4e87-a2a9-ee1b6596d6afhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#f0549536-f0db-4e87-a2a9-ee1b6596d6afGSyrenhttp://social.msdn.microsoft.com/Profile/en-US/?user=GSyrenAutomation vs RaiseEventI have a similar problem. When a button is clicked with the mouse, it shows the down image until the action that was initiated by the button has completed. The down image shows the user that the action is not completed yet. I would like to accomplish the same thing when initiating the button click from code. Is there no simple way to do this?Thu, 02 Apr 2009 12:10:52 Z2009-04-02T12:10:52Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#5ea609ca-7561-49da-9759-e9a9feda3831http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#5ea609ca-7561-49da-9759-e9a9feda3831LoopMhttp://social.msdn.microsoft.com/Profile/en-US/?user=LoopMAutomation vs RaiseEventI've used this on a RepeatButton, and unfortunately it seems that Automation does not honor the Interval property of the RepeatButton. I have to manually enforce the repeat Interval prior to calling the automation.Mon, 01 Jun 2009 18:57:27 Z2009-06-01T18:57:27Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#add887f8-0b10-4846-a304-414e93961242http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1ba3696e-6ca2-48de-80df-31477c9504ce#add887f8-0b10-4846-a304-414e93961242JensNewYorkhttp://social.msdn.microsoft.com/Profile/en-US/?user=JensNewYorkAutomation vs RaiseEventI have almost the same problem with a list box. <br/> I wanted to invoke the selectedvalue methode but there is only the event. <br/> this.lstBox.RaiseEvent(new RoutedEventArgs(ListBox.SelectedEvent)) didn't work. The selectedvalue has still the old value.<br/> I load my date from a database. <br/> <br/> <br/> <br/> <br/> <br/>            <br/>Tue, 07 Jul 2009 22:11:57 Z2009-07-07T22:11:57Z