.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Interview Question >> Windows Application >> Post New Question Subscribe to Interview Questions

How do I host a WPF control in a Windows Forms application?

Posted By :nishithraj     Posted Date :December 28, 2009    Points :10   Category :Windows Application 
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;



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.Dock = DockStyle.Fill;


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.

You can also find related Interview Question to How do I host a WPF control in a Windows Forms application?  below: 

What are the namespaces which needed to host a WPF control in Windows form application

The following namespaces needs to be referenced :

What namespaces are needed to host a WPF control in Windows form application?

The following namespaces needs to be referenced :


How you will Hide Column(s) in the Windows Forms DataGridView Control?

Set the DataGridViewColumn.Visible property to false.

dataGridView1.Columns("CustomerID").Visible = False

dataGridView1.Columns["CustomerID"].Visible = False; (More...)

Can I use Windows Forms in a WPF application?

Yes you can. You can have a WPF application pop a Windows Form much in the same way that you can pop a WPF window from a Windows Forms application. Furthermore, you can place Windows Forms controls side-by-side with WPF controls on a WPF window or page by using the WindowsFormsHost control that will ship as part of the interoperability layer. (More...)

How to disable Cut,Copy and Paste in a Text Box Control C# Windows Application ??

On the text box enter event, please have the below code



What's the safest way to deploy a Windows Forms app?

One click deployment through web. We can implement security checking here. (More...)

Why does my Windows application pop up a console window every time I run it?

Make sure that the target type set in the project properties setting is set to Windows Application, and not Console Application. If you're using the command line, compile with /target:winexe, not /target:exe. (More...)

I can't seem to find System.Windows.Forms.Integration, where do I find it?

The System.Windows.Forms.Integration namespace is defined in WindowsFormsIntegration.dll which currently ships in the WinFX SDK, not in the standard redist. Therefore, the file will be found in "\Program Files\Reference Assemblies\ Microsoft\Avalon\v2.0.50215?. (More...)

Why do we have Databind function in Web application but not in Windows application?

DataGrid ListBox and HTML Select use a collection as a data source. For example listbox uses listitem as a child control but in case of windows there is no list items. When you call DataBind on a parent control it cascades to all of the children of the control. so databind is needed in asp.net not in windows.

Similar is the case with other collection controls like Radio button list and Checkbox list etc (More...)

Different between console application and windows application?

The windows application is graphical user interface(GUI),console application is character user interface(CUI). (More...)

Differences between Windows And Console Application?

http://www.dotnetspark.com/Forum/42-diff-bw-console-application-and-windows.aspx (More...)

How to use a user control in a Web Forms page

Declare the @ Register directive.

For example, use the following code.
<%@ Register TagPrefix="UC" TagName="TestControl" Src="test.ascx" %>

Quick Links For Interview Questions Categories:
ASP.Net Windows Application   .NET Framework   C#   VB.Net   ADO.Net  
Sql Server   SharePoint   Silverlight   OOPs   JQuery   JavaScript/VBScript
Biztalk Patten/Practices .IIS WCF WPF WWF
Networking Aptitude Others   All      

Find questions, FAQ's and their answers related to .NET, C#, Vb.Net, Sql Server and many more.

Now you can find lots of .NET, C#, Vb.Net, SQL Server,Windows, ASP.Net related Questions and their Answers here at www.dotnetspark.com. Our aim is to help you pass your certification Exams (MCP, MCSD, MCAD etc.,) with flying scores and get good name in your company.

So, Start looking our Interview Question section daily and improve your .NET Skills. You can also help others by posting Interview Questions and their Answers in this section.

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend