Put WPF control into a Windows Forms Form

前端 未结 3 867
野的像风
野的像风 2020-11-27 21:00

How do you put a WPF control into a Windows Forms Form? Most likely I will be inserting my WPF control into a Windows.Forms.Panel.

3条回答
  •  庸人自扰
    2020-11-27 21:16

    Summarizing the above answers for quick reference:

    if you don't want to mess with editing the project and want to stick with the designer:

    be sure to add reference for WindowsFormsIntegration.dll which is normally from window's \reference assemblies\microsoft\Framework...

    and if you are using a wpf usercontrol in your solution, you probably already got references to

    System.Windows.Presentation,System.Windows.Activities, System.Windows.CompnentModel, System.Windows..RunTime, System.Windows.WorkFlowServices, System.Xaml.

    otherwise be sure to add the required foregoing references.

    in a windows form member you add the wpf usercontrol myWpfUsrCtl to the windows form as follows

    void addWpfUsrCntl()
    {
        var elemthost1 = new System.Windows.Forms.Integration.ElementHost();
    
        elemthost1.Dock = DockStyle.None; // change to to suit your need
    
         // you can add the WPF control to the form or any other desired control
        elemthost1.Parent = this;
    
        //elemthost1.AutoSize = true; // change to to suit your need
    
        ... // change to to suit your need
    
        elemthost1.Child = myWpfUsrCtl; // Assign the WPF control
    }
    

提交回复
热议问题