We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Platform : WPF
|
Category : WPF in Win32

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

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 :

However, if you want to use XAML to describe the WPF control that you want to use in the Windows Forms application, you 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 have to describe your control. Then you could simply create an instance of this control and add it to the ElementHost control as in the above example :

In addition, you will have to modify the project file because the Windows Application will not know what to do with the XAML file. You will have 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:

Now save this file and reload the project using VS and run the application.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.