【问题标题】:Put WPF control into a Windows Forms Form?将 WPF 控件放入 Windows 窗体窗体?
【发布时间】:2011-06-30 12:34:23
【问题描述】:

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

【问题讨论】:

标签: c# .net wpf visual-studio-2008 .net-3.5


【解决方案1】:

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

例如,请参阅Walkthrough: Hosting a WPF Composite Control in Windows Forms

【讨论】:

  • 你能提供一个简短的例子吗..?
【解决方案2】:

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

如果你不想搞乱编辑项目并想坚持设计者:

请务必添加对WindowsFormsIntegration.dll 的引用,该引用通常来自窗口的\reference assembly\microsoft\Framework...

如果您在解决方案中使用 wpf 用户控件,您可能已经获得了对

的引用

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

否则请务必添加所需的上述引用。

在 windows 窗体成员中,您将 wpf 用户控件 myWpfUsrCtl 添加到 windows 窗体,如下所示

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_ 是什么?
【解决方案3】:

尝试阅读以下内容:
在 Windows 窗体应用程序中托管 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 窗体应用程序的控件,然后将该控件连接到 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 窗体应用程序中使用的 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

【讨论】:

  • 阿瓦隆……那是很久以前的事了……我差点忘了
  • 其实host.Controls是一个System.Windows.Forms.Control.ControlCollection。使用 host.Child = wpfControl 在 windows 窗体中导入 wpf 控件。
  • “添加对 WPF 命名空间的引用”是什么意思?这些实际上是应该引用的特定 DLL 的名称吗?
  • 链接失效
猜你喜欢
  • 1970-01-01
  • 2012-05-31
  • 2013-12-20
  • 1970-01-01
  • 2016-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多