0
votes

How to add a wpf usercontrol within a winforms usercontrol? Tried hosting the Wpf usercontrol using the element host as below.The added child control doesn't show at design and runtime.

public partial class WinformsUC: UserControl
{
    public WinformsUC()
    {
        InitializeComponent();
        try
        {
           var eH = new ElementHost {Dock = DockStyle.Fill, Child = new WpfUserControl()};
           Controls.Add(eH);
        }
        catch (Exception exception)
        {
            Log.Debug(Constructor : " + exception.Message);
        }
    }
}
1
@user1874589 Are you sure you added Windows usercontrol to the main windows form and added some content within the WPFUsercontrol? It works good for me with your code.Siva Gopal
Yes thats a windows usercontrol not a wpf usercontrol and WPF usercontrol has wpf controls as contents.user1874589

1 Answers

0
votes

you can do that using the system.windows.forms.integration.elementhost

Example :

private ElementHost ctrlHost;
private UIElement wpfUserControl;

// in the from the load event handler
ctrlHost = new ElementHost();
ctrlHost.Dock = DockStyle.Fill;
panel1.Controls.Add(ctrlHost);
wpfUserControl= new WpfUserControl();

//we need to manually call initializecomponent if it is not called
//in the constructor of your control
wpfUserControl.InitializeComponent();
ctrlHost.Child = wpfInputControl;