button1.PerformClick() in wpf

后端 未结 7 1592
难免孤独
难免孤独 2020-12-15 21:07

Why this code in WPF does not work ?

private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(\"yes\");
    }
    pr         


        
相关标签:
7条回答
  • 2020-12-15 21:59

    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#

    0 讨论(0)
  • 2020-12-15 22:00

    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();
    
    0 讨论(0)
  • 2020-12-15 22:07

    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();
    }
    
    0 讨论(0)
  • 2020-12-15 22:08

    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);
    
    0 讨论(0)
  • 2020-12-15 22:11

    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();
    
    0 讨论(0)
  • 2020-12-15 22:11

    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);         
    }
    
    0 讨论(0)
提交回复
热议问题