如何将WPF用户控件放在WInforms usercontrol中?

时间:2015-09-22 11:06:03

标签: wpf winforms

如何在 winforms usercontrol 中添加 wpf usercontrol ?尝试使用元素主机托管Wpf用户控件,如下所示。添加的子控件不会在设计和运行时显示。

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 个答案:

答案 0 :(得分:0)

你可以使用system.windows.forms.integration.elementhost

来做到这一点

示例:

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;