wpf中的button1.PerformClick()

时间:2011-01-19 11:03:38

标签: c# wpf

为什么WPF中的代码不起作用?

private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("yes");
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        button1.PerformClick();
    }

我需要命令。

7 个答案:

答案 0 :(得分:24)

要使用Windows窗体应用程序的样式,您需要编写以下扩展方法:

namespace System.Windows.Controls
{
    public static class MyExt
    {
         public static void PerformClick(this Button btn)
         {
             btn.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
         }
    }
}

现在你可以将它用于任何按钮,假设一个名为“btnOK”的按钮:

btnOK.PerformClick();

答案 1 :(得分:9)

等等..有简单的方法。如果你的按钮名称是 button1 并且按钮1点击了已订阅的事件,你只需要调用该事件

button1_Click(this,null);

答案 2 :(得分:5)

取代 PerformClick(),而不是 RaiseEvent()

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("yes");
}
private void Form1_Load(object sender, EventArgs e)
{
    RoutedEventArgs newEventArgs = new RoutedEventArgs(Button.ClickEvent);
    button1.RaiseEvent(newEventArgs);         
}

答案 3 :(得分:3)

WPF中的好习惯是使用命令。它提高了可测试性并分离了UI和业务逻辑。

首先,您可以尝试RoutedUICommand。

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:self ="clr-namespace:Test"
    Title="MainWindow" 
    Height="350" Width="525">
<Window.CommandBindings>
    <CommandBinding Command="{x:Static self:MainWindow.RoutedClickCommand}"
                    CanExecute="CommandBinding_CanExecute"
                    Executed="CommandBinding_Executed"/>
</Window.CommandBindings>
<Grid>
    <Button Content="Test" Name="Btn1" Command="{x:Static self:MainWindow.RoutedClickCommand}"/>
</Grid>

在代码隐藏文件中,我们必须定义RoutedClickCommand和Execute | CanExecute处理程序:

    public static ICommand RoutedClickCommand = new RoutedUICommand("ClickCommand", "ClickCommand", typeof(MainWindow));

    private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }

    private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("ololo");
    }

因此,当您需要按钮逻辑(样本中的“button1.PerformClick();”)时,只需输入下一行:

MainWindow.RoutedClickCommand.Execute(null);

对于我来说,我提出了另一种假设将命令带入演示模型的方法。复合应用程序库(Prism)帮助我使用DelegateCommand类。然后,演示模型中的命令定义如下:

    private DelegateCommand<object> _clickCommand;

    public ICommand ClickCommand
    {
        get
        {
            if (this._clickCommand == null)
            {
                this._clickCommand = new DelegateCommand<object>(p =>
                    {
                        //command logic
                    },
                    p =>
                    { 
                        // can execute command logic
                    });
            }
            return this._clickCommand;
        }
    }

查看XAML和代码:

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:self ="clr-namespace:Test"
    Title="MainWindow" 
    Height="350" Width="525">
<Grid>
    <Button Content="Test" Name="Btn1" Command="{Binding ClickCommand}"/>
</Grid>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Model = new SampleModel();
    }

    protected SampleModel Model
    {
        get
        {
            if (this.Model.ClickCommand.CanExecute())
            {
                this.Model.ClickCommand.Execute();
            }
            return (SampleModel)this.DataContext;   
        }
        set 
        {
            this.DataContext = value;
        }
    }
}

下一个代码在视图中调用命令,绕过单击按钮:

if (this.Model.ClickCommand.CanExecute())
{
 this.Model.ClickCommand.Execute();
}

答案 4 :(得分:3)

Adam Nathans推荐的 WPF Unleashed blog的摘录。
Imho是最好的,如果不是最好的WPF参考。

var bap = new System.Windows.Automation.Peers.ButtonAutomationPeer(someButton);
var iip = bap.GetPattern(System.Windows.Automation.Peers.PatternInterface.Invoke)  
                            as System.Windows.Automation.Provider.IInvokeProvider;
iip.Invoke();

答案 5 :(得分:2)

我认为解决问题的最短,最有效的解决方案只需一行即可完成。

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

这应该适用于WPF C#

答案 6 :(得分:1)

因为PerformClick是WindowsForms Button控件的一个方法:

http://msdn.microsoft.com/en-us/library/system.windows.forms.button.performclick.aspx

不在WPF按钮控件上:

http://msdn.microsoft.com/en-us/library/system.windows.controls.button_methods.aspx

要自动执行按钮单击,您可能需要查看UI自动化框架:

http://msdn.microsoft.com/en-us/library/ms747327.aspx

相关问题