Search code examples
c#winformsuser-controlselementhostbackcolor

Black backcolor in Controls when it was set to transparent


I´m having an issue with WPF elementHost backcolors. I have a winform that loads in a panel an UserControl(Winform). In that user control I have two panels, inside of each of them, there is an elementHost that hosts a WPF usercontrol.

The picture can be more helpful: enter image description here

As you can see, the backcolor of each of them is BLACK while the element should be displayed like this:

enter image description here

I notice that this happened when I load other UserControls in panels. I need to know how to fix this.

Each elementHost has its backcolor set to Transparent and BackColorTransparent in True. Also, I tried to change the backcolor in runtime, but still the same problem.

UPDATE: Ok guys, I notice that if I load the UserControl in the panel in the event Load of the Form, the elementHost loads correctly. But, if I load the UserControl in the panel in a button click event, I´m getting that black backcolor.

WORKS:

private void frm_Configuracion_Load(object sender, EventArgs e)
    {
        /*ABM.frm_ABM_Banco_Sucursal banco_sucursal = new ABM.frm_ABM_Banco_Sucursal();
        panel1.Controls.Add(banco_sucursal);*/
    }

DONT WORK:

private void button3_Click(object sender, EventArgs e)
    {
        ABM.frm_ABM_Banco_Sucursal banco_sucursal = new ABM.frm_ABM_Banco_Sucursal();
        panel1.Controls.Add(banco_sucursal);
    }

Obviously, using the Load event would be the solution just if I´m using one UserControl, but I´m not.


Solution

  • Ok, this is how I solve it:

    I put this code in the Load() event of every UserControl:

    elementHost.BackColorTransparent = true;
    

    Being elementHost every elementHost element that is having that trouble.

    Its curious, but that property was set in true in design time using the visual designer, I think something could mess it up during execution.