简体   繁体   中英

C# All WPF controls using a common event in Windows Form

I have created a custom WPF control like this:

自定义WPF按钮

I placed this WPF control in my Form1 (Windows Form) and wrote a event MouseEnter for it. What I expect here is to get the name of the button inside WPF control. I did it with only 1 WPF control in Form1. But I have 24 WPF controls in Form1. I used foreach to loop through all ElementHost (WPF controls) and cast the child of each ElementHost to WPF control. I have an issue to cast ElementHost to WPF controls.

Here is my code:

   public Form1()
        {
            InitializeComponent();
            foreach (ElementHost c in this.Controls)
            {        

                     TP1WPFControls.TP1CustomButton bt = c.Child as TP1CustomButton;

                    bt.bt.MouseEnter += Bt_MouseEnter;    
                    bt.bt.MouseLeave += Bt_MouseLeave;
            }

            //TP1WPFControls.TP1CustomButton btnCustom = elementHost1.Child as TP1CustomButton;
            //btnCustom.bt.Click += Bt_Click;
            //btnCustom.bt.MouseEnter += Bt_MouseEnter; ;
            //btnCustom.bt.MouseLeave += Bt_MouseLeave; ;

        }

And this is my picture to show the WPF controls in Form1: 在此处输入图片说明

Hope everyone can give me a solution for this.

Thank you!

You need to loop through the form controls, but not all of them are element hosts:

Each Windows Forms control is based off of System.Windows.Forms.Control including ElementHost. So, in the loop, you try to cast to that. If it's not null, you know its an ElementHost, then you can do your code on the ElementHost.

foreach (System.Windows.Forms.Control e in this.Controls)
{        

    ElementHost c= e as ElementHost;
    if ( c != null ) 
    {
        TP1WPFControls.TP1CustomButton bt = c.Child as TP1CustomButton;
        if ( bt != null )
        {
                // Add Events (I'd assume it would just be bt.MouseEnter but I don't know what your control looks like)
                bt.bt.MouseEnter += Bt_MouseEnter;    
                bt.bt.MouseLeave += Bt_MouseLeave;
        }
    }
}

Thank you so much @Ctznkane525. I tried to cast the sender in MouseLeave and MouseEnter event to the type of each control in WPF custom control and It has worked! Because my TPCustomButton is just an WPF control so I have to cast each control inside it for each event above.

I have edited my answer for full information:

 public Form1()
        {
            InitializeComponent();
            foreach (Control c in this.Controls)
            {
                ElementHost e = c as ElementHost;
                if (e != null)
                {

                    TP1WPFControls.TP1CustomButton btWPF = e.Child as TP1CustomButton;
                    if (btWPF != null)
                    {

                        //bt.bt.MouseLeave += Bt_MouseLeave;
                        //bt.lbl.MouseLeave += Lbl_MouseLeave;
                        btWPF.MouseLeave += Bt_MouseLeave;
                    }
                }
            }


        }
   private void Bt_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            TP1CustomButton btCustom = sender as TP1CustomButton;


            brushcolor = new brushColor(mediaColor.FromRgb(221, 247, 190));
            btCustom.bt.Background = brushcolor;
            btCustom.lbl.Background = brushcolor;

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM