如何以编程方式单击WPF中的按钮?

时间:2009-04-08 03:32:01

标签: c# .net wpf button

由于WPF中没有button.PerformClick()方法,有没有办法以编程方式单击WPF按钮?

8 个答案:

答案 0 :(得分:177)

就像JaredPar所说,你可以参考Josh Smith关于自动化的文章。但是,如果您通过对他的文章的评论,您会发现针对WPF控件提升事件的更优雅方式

someButton.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));

我个人更喜欢上面的那个而不是自动化同行。

答案 1 :(得分:120)

WPF采用的方法与WinForms略有不同。它们不是将对象的自动化内置到API中,而是为每个负责自动化对象的对象提供单独的类。在这种情况下,您需要ButtonAutomationPeer来完成此任务。

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

Here是关于此主题的博文。

注意:IInvokeProvider接口是在UIAutomationProvider程序集中定义的。

答案 2 :(得分:28)

如果你想调用点击事件:

SomeButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

如果你想让按钮看起来像是按下了:

typeof(Button).GetMethod("set_IsPressed", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(SomeButton, new object[] { true });
之后

并且没有留下:

typeof(Button).GetMethod("set_IsPressed", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(SomeButton, new object[] { false });

或使用ToggleButton

答案 3 :(得分:19)

this.PowerButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

答案 4 :(得分:5)

以编程方式“单击”按钮的一种方法是,只要调用按钮的OnClick事件处理程序(或执行与按钮关联的ICommand,如果您在更多WPF中执行操作) -y方式)。

你为什么要这样做?例如,您是在进行某种自动化测试,还是尝试从不同的代码段执行按钮执行的相同操作?

答案 5 :(得分:4)

正如Greg D所说,我认为使用MVVM模式(单击事件引发和命令执行)单击按钮Automation的替代方法是使用反射调用OnClick方法:

typeof(System.Windows.Controls.Primitives.ButtonBase).GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(button, new object[0]);

答案 6 :(得分:0)

MVVM 命令模式用于 Button 功能(推荐做法)时,触发 Button 效果的一种简单方法如下:如下:

someButton.Command.Execute(someButton.CommandParameter);

这将使用按钮触发的 Command 对象,并传递由 XAML 定义的 CommandParameter

答案 7 :(得分:0)

Automation API解决方案的问题在于,它需要引用Framework程序集UIAutomationProvider作为项目/程序包的依赖项。

另一种方法是模仿行为。以下是我的扩展解决方案,该解决方案还用其绑定的命令约束MVVM模式-以扩展方法实现:

public static class ButtonExtensions
{
    /// <summary>
    /// Performs a click on the button.<br/>
    /// This is the WPF-equivalent of the Windows Forms method "<see cref="M:System.Windows.Forms.Button.PerformClick" />".
    /// <para>This simulates the same behaviours as the button was clicked by the user by keyboard or mouse:<br />
    /// 1. The raising the ClickEvent.<br />
    /// 2.1. Checking that the bound command can be executed, calling <see cref="ICommand.CanExecute" />, if a command is bound.<br />
    /// 2.2. If command can be executed, then the <see cref="ICommand.Execute(object)" /> will be called and the optional bound parameter is p
    /// </para>
    /// </summary>
    /// <param name="sourceButton">The source button.</param>
    /// <exception cref="ArgumentNullException">sourceButton</exception>
    public static void PerformClick(this Button sourceButton)
    {
        // Check parameters
        if (sourceButton == null)
            throw new ArgumentNullException(nameof(sourceButton));

        // 1.) Raise the Click-event
        sourceButton.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent));

        // 2.) Execute the command, if bound and can be executed
        ICommand boundCommand = sourceButton.Command;
        if (boundCommand != null)
        {
            object parameter = sourceButton.CommandParameter;
            if (boundCommand.CanExecute(parameter) == true)
                boundCommand.Execute(parameter);
        }
    }
}
相关问题