如何以编程方式单击WPF中的按钮? [英] How to programmatically click a button in WPF?

查看:148
本文介绍了如何以编程方式单击WPF中的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于有在WPF中没有 button.PerformClick()的方法,是有办法来点击一个WPF按钮编程?

Since there's no button.PerformClick() method in WPF, is there a way to click a WPF button programmatically?

推荐答案

WPF需要一个性能稍微不同的方式比的WinForms这里。代替具有内置到API一个对象的自动​​化的,它们具有对于每个对象,它是负责自动化它一个单独的类。在这种情况下,你需要ButtonAutomationPeer来完成这项任务。

WPF takes a slighly different approach than WinForms here. Instead of having the automation of a object built into the API, they have a separate class for each object that is responsible for automating it. In this case you need the ButtonAutomationPeer to accomplish this task.

ButtonAutomationPeer peer =
  new ButtonAutomationPeer( someButton );
IInvokeProvider invokeProv =
  peer.GetPattern( PatternInterface.Invoke )
  as IInvokeProvider;
invokeProv.Invoke();

下面是关于这一主题的博客文章:<一个href="http://joshsmithonwpf.word$p$pss.com/2007/03/09/how-to-programmatically-click-a-button/">http://joshsmithonwpf.word$p$pss.com/2007/03/09/how-to-programmatically-click-a-button/

Here is a blog post on the subject: http://joshsmithonwpf.wordpress.com/2007/03/09/how-to-programmatically-click-a-button/

这篇关于如何以编程方式单击WPF中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆