button1.PerformClick() in wpf

后端 未结 7 1619
难免孤独
难免孤独 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 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();
    

提交回复
热议问题