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
.
Put an ElementHost
control inside the panel. This control can then host a WPF element. From the WinForms designer, you can find this control under 'WPF Interoperability'. First you may need to add WindowsFormsIntegration.dll
to your project's references.
For an example, see Walkthrough: Hosting a WPF Composite Control in Windows Forms.
Try to read this:
Hosting a WPF Control in a Windows Forms Application
http://community.infragistics.com/wpf/articles/hosting-a-wpf-control-in-a-windows-forms-application.aspx
First add references to the WPF namespaces (PresentationCore, PresentationFramework, UIAutomationProvider, UIAutomationTypes, and WindowsBase). Next create an instance of the ElementHost control and the control you wish to embed in the Windows Forms application and then hook that control up to the ElementHost control. Then simply add the ElementHost control to your Forms control collection:
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);
However, if you want to use XAML
to describe the WPF control that you want to use in the Windows Forms application, you would need to add an Avalon UserControl item to your project. This will create a UserControl1.xaml
file and a UserControl1.xaml.cs
file. You can then modify the UserControl1.xaml file to contain whatever XAML you wish to describe your control. Then you would simply create an instance of this control and add it to the
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);
In addition, you will need to modify the project file because the Windows Application does not what to do with the XAML file. You will need to open the project file (.csproj, .vbproj, etc.) in an editor like Notepad and then scroll to the bottom. You will see the following line:
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
You will need to copy this line and paste it just below the above line and then change "CSharp" to "WinFX" so that the two lines look like:
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.WinFx.targets" />
Now save this file and reload the project using VS and run the application.
Source: http://windowsclient.net/learn/integration.aspx
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
}
精彩评论