Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » WPF » Integrating WPF in WinForms in VS 2010

Integrating WPF in WinForms in VS 2010

In this article we will see how we can integrate WPF User control into Winform application in Visual Studio 2010.

Author Rank :
Page Views : 26570
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Introduction

In this article we will see how we can integrate WPF User control into Winform application in Visual Studio 2010.

Creating Blank Solution

Fire up Visual Studio 2010 and create a Blank Solution, name the solution as WPF.Winform.Integration.

1.gif

Let's add a Winform Application and give it a name "WinformSample". As we are integrating WPF into Winforms so make sure this is your start up Project for the solution.

2.gif

Let's see simplest thing what we can do. We can add two properties of an entity from the Winform to the WPF. So I have designed the following Winform with two text boxes (Name and Contact) and then a Button to add to the UserControl.

3.gif 

To host a WPF User Control, we need a control called "Element Host".

4.gif 

Add one instance of the Element Host into the application. Resize it accordingly.

5.gif 

Now, let's add a WPF Application to the solution, name the project as WPFSample.

6.gif 

Now that we have added the the WPF Application, the following is the solution structure.

7.gif 

Now, the most important part, add the project reference of the WPF Application to the Winforms Application.

8.gif 

The following xaml represents the User Control.

9.gif 

In Winforms design page, if you see the Element Host control, you can find a option fly on the top right corner. Click on that to see the options.

10.gif 

Click on the drop down, and you should see your user control. If you are unable to see the User Control, clean and rebuild your solution one more time.

11.gif 

Select the User Control and it would be added to the Element Host.

12.gif
 
Now that we have added the User Control to the Winforms application. Let's write some code that would reflect from Winform application to WPF User Control.

We have to define a class that would represent our Entity, so let's add a class and name it as Person. 

13.gif 

The following is the class structure for the Entity Person.

14.gif 

Now we should have a method, that can be accessed from Winform applciation. The following method refreshes the ItemsSource property and set it.

15.gif 

Go back to the Winforms Application code behind and on the Button click of Add Button, write the following lines of code.

16.gif 

That's it, lets run the application.
 
17.gif

18.gif 

We are able to communicate from Winforms to WPF User Control.

Hope this article helps.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Diptimaya Patra

Diptimaya is working as a Sr. Software Engineer in Microsoft Technologies (C#). He is a Microsoft MVP in Client App Dev, he has a good hands on in Silverlight 2/3/4, WPF 3/4, Expression Blend 3/4.


Follow him in Twitter: http://www.twitter.com/dpatra

Blog: http://dpatra.blogspot.com , http://diptimayapatra.wordpress.com

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Good article by Mahesh On July 6, 2010
Keep up the good work Dipti. Image 2 is missing.
Reply | Email | Modify 
Nevermind - I see images by Mahesh On July 6, 2010
I see it. Must be loading first time.
Reply | Email | Modify 
Launch & access Win form from Silverlight by N On September 28, 2010
Hi,

Thanks; the article is helpful. I am looking for launching and accessing winform from Silverlight; is it possible? If no, are there any workarounds? I know it is possible to launch an ActiveX from Silverlight. Can you point me to a link that explains the steps required to achieve this?

Thanks
Reply | Email | Modify 
A few small suggestions by Mike On November 8, 2010
I think there is a step missing. After "Now, the most important part, add the project reference of the WPF Application to the Winforms Application." you need to add a reference to the assembly in the Form1.Designer.cs.

Also I think the code for the control could be simplified :

public partial class PersonList : UserControl
{
   List<Person> PeopleList = new List<Person>(); // Create a people list

   public PersonList()
   {
       InitializeComponent();
   }
   public void SetItemsSource(Person oPerson)
   {
      PeopleList.Add(oPerson);
      lbPerson.ItemsSource =
null;
      lbPerson.ItemsSource = PeopleList;
   }
}

Reply | Email | Modify 
possibility of controlling from wpf control to winforms ? by vamshi kiran On January 20, 2011
HI Nice article. Is it possible if we do some action on wpf control & winforms shud have some response ?
Reply | Email | Modify 
Where does it come from personsList1 Object in btnAdd_Click()?? by Shin On March 22, 2011
Where does it come from personsList1 Object in btnAdd_Click()??
Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.