http://joshsmithonwpf.wordpress.com/2007/03/09/how-to-programmatically-click-a-button/
Back in WinForms it was really easy to simulate a userclicking a Button, you just call a Button’s PerformClick method. For somereason the WPF Button does not have that method. It turns out that youneed to enter into the automation world to accomplish this task.
WPF takes a slighly different approach than WinForms here.Instead of having the automation of an object built into the API, they have aseparate class for each object that is responsible for automating it. In thiscase you need the ButtonAutomationPeer to accomplish this task.
ButtonAutomationPeer peer = new ButtonAutomationPeer(someButton );
IInvokeProviderinvokeProv = peer.GetPattern( PatternInterface.Invoke ) as IInvokeProvider;
invokeProv.Invoke();
The UIAutomation*.dll is located in this folder:
C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0
If you can't find in your Add Reference->.Net tab, then you have to use the Browse tabto go to the given path, and add the assembly (Right Click on the References, choose add reference, clickbrowse tab):
Other reference in Chinese:

本文介绍了在WPF中通过ButtonAutomationPeer类来模拟用户点击按钮的操作,包括找到按钮对象、创建自动化代理并调用Invoke方法实现点击。详细步骤及所需组件路径都在文中列出。
726

被折叠的 条评论
为什么被折叠?



