开发者

button1.PerformClick() in wpf

Why this code in WPF does not work ?

private void button开发者_StackOverflow中文版1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("yes");
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        button1.PerformClick();
    }

I need to command.


To use the windows form application's style, you need to write the following extension method:

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

now you can use it for any button, assuming a button called "btnOK":

btnOK.PerformClick();


Wait.. there is simple way. if your button name is button1 and button1 click event already subscribed,you will just call that event like

button1_Click(this,null);


Instead of PerformClick() use 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);         
}


I think the shortest and most efficient solution to your problem would be simply done in one line.

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

That should work for WPF C#


Good practice in WPF is using commands. It improves testability and separates UI and business logic.

First you may try 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>

In code behind file we have to define RoutedClickCommand and Execute|CanExecute handlers:

    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");
    }

So, when you need button logic ("button1.PerformClick();" in your sample), just put next line:

MainWindow.RoutedClickCommand.Execute(null);

As for me, I preffer another way which supposes carry command into presentation model. Composite Application Library (Prism) helps me with its DelegateCommand class. Then command definition in presentation model looks like:

    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;
        }
    }

And view XAML and code behind:

<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;
        }
    }
}

Next code calls command in view bypassing clicking on button:

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


An excerpt from Adam Nathans WPF Unleashed, recommended by this blog.
Imho one of the best, if not the best WPF references around.

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();


Because PerformClick is a method on WindowsForms Button control:

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

Not on the WPF Button control:

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

To automate a button click, you might like to take a look at the UI automation framework:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜