Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将WPF控件放入Windows窗体中_C#_.net_Wpf_Visual Studio 2008_.net 3.5 - Fatal编程技术网

C# 将WPF控件放入Windows窗体中

C# 将WPF控件放入Windows窗体中,c#,.net,wpf,visual-studio-2008,.net-3.5,C#,.net,Wpf,Visual Studio 2008,.net 3.5,如何将WPF控件放入Windows窗体中?我很可能将WPF控件插入Windows.Forms.Panel 在面板内放置一个控件。然后,此控件可以承载WPF元素。从WinForms设计器中,您可以在“WPF互操作性”下找到此控件。首先,您可能需要将WindowsFormsIntegration.dll添加到项目的引用中 有关示例,请参见。尝试阅读以下内容: 在Windows窗体应用程序中托管WPF控件 首先添加对WPF名称空间的引用(PresentationCore、Presentation

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

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

有关示例,请参见。

尝试阅读以下内容:
在Windows窗体应用程序中托管WPF控件


首先添加对WPF名称空间的引用(PresentationCore、PresentationFramework、UIAutomationProvider、UIAutomationTypes和WindowsBase)。接下来,创建ElementHost控件和要嵌入Windows窗体应用程序中的控件的实例,然后将该控件连接到ElementHost控件。然后只需将ElementHost控件添加到窗体控件集合:

    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);
此外,您还需要修改项目文件,因为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重新加载项目,然后运行应用程序




资料来源:

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

如果您不想在编辑项目时遇到麻烦,并且希望与设计师保持联系,请执行以下操作:

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

如果您在解决方案中使用wpf usercontrol,您可能已经获得了对的引用

System.Windows.Presentation、System.Windows.Activities、, System.Windows.ComponentModel,System.Windows..RunTime,System.Windows.WorkFlowServices,System.Xaml

否则,请务必添加所需的上述参考资料

在windows窗体成员中,将wpf usercontrol 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
}

你能举个简短的例子吗?阿瓦隆。。。那是很久以前的事了。。。我几乎忘记了host.Controls实际上是一个System.Windows.Forms.Control.ControlCollection。使用host.Child=wpfControl在windows窗体中导入wpf控件。您所说的“添加对wpf命名空间的引用”是什么意思?这些实际上是应该引用的特定DLL的名称吗?链接是dead在示例中是什么?
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
}