binh nguyen
binh nguyen

Reputation: 127

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

I have created a custom WPF control like this:

Custom WPF Button

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: enter image description here

Hope everyone can give me a solution for this.

Thank you!

Upvotes: 0

Views: 469

Answers (2)

binh nguyen
binh nguyen

Reputation: 127

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;

        }

Upvotes: 0

Ctznkane525
Ctznkane525

Reputation: 7465

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

Upvotes: 1

Related Questions