将WPF控件放入Windows Forms表单中?

32

如何将WPF控件放入Windows Forms的 Form中? 很可能我会将我的WPF控件插入到Windows.Forms.Panel中。


此问题的副本:https://stackoverflow.com/questions/2865567/net-how-to-use-a-wpf-user-control-in-a-windows-forms-application - StayOnTarget
另一个:https://dev59.com/InA65IYBdhLWcg3wrgwa - StayOnTarget
3个回答

39

ElementHost控件放置在面板内。此控件可以托管WPF元素。从WinForms设计器中,您可以在“WPF互操作性”下找到此控件。首先,您可能需要将WindowsFormsIntegration.dll添加到项目的引用中。

有关示例,请参见演练:在Windows Forms中托管WPF复合控件


4
可以,请提供一个简短的例子吗? - Darkhydro
这个在WinForms应用程序中使用的WPF控件会使用DirectX吗? - undefined

4
尝试阅读以下内容:
在Windows Forms应用程序中托管WPF控件
http://community.infragistics.com/wpf/articles/hosting-a-wpf-control-in-a-windows-forms-application.aspx

首先,添加对WPF命名空间(PresentationCore、PresentationFramework、UIAutomationProvider、UIAutomationTypes和WindowsBase)的引用。接下来创建ElementHost控件的实例和您希望嵌入到Windows Forms应用程序中的控件,然后将该控件连接到ElementHost控件。然后只需将ElementHost控件添加到您的Forms控件集合即可。
    ElementHost host = new ElementHost();
    System.Windows.Controls.ListBox wpfListBox = new System.Windows.Controls.ListBox();
    for (int i = 0; i < 10; i++)
    {
    wpfListBox.Items.Add("Item " + i.ToString());
    }
    host.Dock = DockStyle.Fill;
    host.Controls.Add(wpfListBox);
    this.panel1.Controls.Add(host);

但是,如果你想使用XAML来描述在Windows Forms应用程序中使用的WPF控件,你需要在项目中添加Avalon UserControl项。这将创建一个UserControl1.xaml文件和一个UserControl1.xaml.cs文件。然后你可以修改UserControl1.xaml文件,以包含任何你想要用来描述你的控件的XAML代码。接着你只需要创建该控件的实例并将其添加到

ElementHost control as in the above example:
ElementHost host = new ElementHost();
UserControl1 uc1 = new UserControl1();
host.Controls.Add(uc1);
host.Dock = DockStyle.Fill;
this.panel1.Controls.Add(host);

此外,您需要修改项目文件,因为Windows应用程序不知道如何处理XAML文件。您需要使用记事本等编辑器打开项目文件(.csproj、.vbproj等),然后滚动到底部。您会看到以下行:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

您需要复制这一行并将其粘贴到上面的行下面,然后将“CSharp”更改为“WinFX”,使得这两行看起来像:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.WinFx.targets" />

现在保存这个文件,重新载入使用VS的项目并运行应用程序。
来源:http://windowsclient.net/learn/integration.aspx

Avalon...那是很久以前的事了...我都差点忘记了。 - Emond
4
实际上,host.Controls是System.Windows.Forms.Control.ControlCollection。要将WPF控件导入Windows Forms中,请使用host.Child = wpfControl。 - MrFox
“添加对WPF命名空间的引用”是什么意思?这些实际上是应该被引用的特定DLL的名称吗? - StayOnTarget
1
链接已失效。 - StayOnTarget

1

总结上述答案以供快速参考:

如果您不想编辑项目并希望坚持使用设计师:

请确保添加对WindowsFormsIntegration.dll的引用,该文件通常来自Windows的\reference assemblies\microsoft\Framework...

如果您在解决方案中使用WPF用户界面控件,则可能已经获取了以下引用:

System.Windows.Presentation、System.Windows.Activities、System.Windows.CompnentModel、 System.Windows..RunTime、System.Windows.WorkFlowServices、System.Xaml。

否则,请确保添加所需的上述引用。

在Windows窗体成员中,您可以使用以下方式将WPF用户界面控件myWpfUsrCtl添加到窗体中。

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
}

在这个例子中,FinCurl_是什么? - StayOnTarget

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接