Articles in this section
Category / Section

How to host WPF control in Windows Forms project?

3 mins read

Host WPF control in Windows Forms project

It is possible to use Syncfusion WPF controls in Windows Forms Application by following below steps.

Step 1: Create Windows Forms and Windows Forms Control Library Application.

Screenshot

Show the Windows Forms application

Figure 1: Windows Forms Application

Need to create the Windows Control Library application as like in below.

Screenshot

Show the Windows Forms control library application

Figure 2: Windows Forms Control Library Application

Step 2: Configure the required WPF assemblies

Need to add UserControl.Xaml file in Windows Forms Control Library Application and required Syncfusion WPF dependency assemblies.

For example,

Here we are using the Syncfusion SfAccordion WPF control. So, it is needed to add UserControl.xaml file into the project.

Screenshot

Add Xaml file into new application

Figure 3: Adding UserControl.Xaml file into the WindowsControlLibrary application.

Then it is needed to add the necessary configuration for SfAccordion WPF control in the loaded UserControl.Xaml file. The following code snippets explain the same.

Xaml

<UserControl x:Class="WindowsFormsControlLibrary1.SyncAccordion"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"              
             xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
             d:DesignHeight="152" d:DesignWidth="629">
    <Grid>
        <syncfusion:SfAccordion x:Name="accordion">
            <syncfusion:SfAccordionItem Header="Windows Phone"/>
            <syncfusion:SfAccordionItem Header="Windows Forms"/>
            <syncfusion:SfAccordionItem Header="Windows Presentation Foundation"/>
            <syncfusion:SfAccordionItem Header="WinRT"/>
            <syncfusion:SfAccordionItem Header="Universal Windows Platform"/>
        </syncfusion:SfAccordion>
    </Grid>
</UserControl>

Step 3: Need to create Element Host class.

To load WPF control in Windows Forms application, it is needed to implement Class derived from ElementHost and load SfAccordion WPF control in it. The following code snippet explains the same.

C#

[Designer("System.Windows.Forms.Design.ControlDesigner, System.Design")]
[DesignerSerializer("System.ComponentModel.Design.Serialization.TypeCodeDomSerializer , System.Design", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design")]
public class SyncAccordionGrid : System.Windows.Forms.Integration.ElementHost
{
   protected SyncAccordion m_WPFSyncAccordion = new SyncAccordion();
   public SyncAccordionGrid()
   {
      base.Child = m_WPFSyncAccordion;     
   } 
}

VB

<Designer("System.Windows.Forms.Design.ControlDesigner, System.Design"), DesignerSerializer("System.ComponentModel.Design.Serialization.TypeCodeDomSerializer , System.Design", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design")>
Public Class SyncAccordionGrid
    Inherits System.Windows.Forms.Integration.ElementHost
    Protected m_WPFSyncAccordion As New SyncAccordion()
      Public Sub New()
         MyBase.Child = m_WPFSyncAccordion
      End Sub
End Class

Step 4: Need to attach Windows Control Library project into Windows Forms application.

It is needed to attach the created Windows Control Library project into Windows Forms application and add it to its assembly reference section.

Step 5: Need to add SfAccordion control in Windows Forms application.

As both the applications are merged, it is needed to rebuild the whole application. It will add SfAccordion WPF control in Windows Forms Application Designer Page Toolbox. It can be dragged and dropped into the Form Designer.

Screenshot

  Loading WPF Accordion control into WF application

 Figure 4: Loading WPF SfAccordion control into Windows Forms application.

And now it can be used in Windows Forms application.

Screenshot

Accordion control can be used in WF application

   Figure 5:  SfAccordion control can be used in Windows Forms Application.

Note:

To run the Application and open the Form Designer, it is needed to rebuild this application.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied