How Can I Import one .net framework WPFControl or one .net5 WPFControl into The new .net5 Winform?

Shawn Song 41 Reputation points

I've tried to used a .net framework WPFUserControl into the .net5 WPF (.net framework WPF certainly too) , and how can I use this control into my .NET5 Winform? I guess .NET5 Winform can't use ElementHost anymore.right?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,820 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,666 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points

    No, you can use ElementHost in WinForm to host WPF Control in .Net5, but you need to use the newer version of 2019. I test a .Net5 WinForm project to host WPF DataGrid in Visual Studio Enterprise 2019 Version 16.8.1.
    Here is my steps and code for you:
    Step 1: Create a .Net5 Winform project named WPNet5 and make it's .csproj as below:

    <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">  

    Step 2: Add a WPF UserControl named WPFControls

    Step 3: Code for WPFControls.xaml

            <DataGrid x:Name="dataGrid" ItemsSource="{Binding }" AutoGenerateColumns="False"  SelectionMode="Extended" SelectionUnit="CellOrRowHeader" >  
                    <DataGridTextColumn Header="Name" Width="80" Binding="{Binding Name}"/>  
                    <DataGridTextColumn Header="Age" Width="50" Binding="{Binding Age}" />  
                    <DataGridHyperlinkColumn Header="Email" Width="150"   Binding="{Binding Email}"/>  


     public partial class WPFControls : UserControl  
                public WPFControls()  
                    ObservableCollection<Member> memberData = new ObservableCollection<Member>();  
                    Random radom = new Random();  
                    for (int i = 1; i < 20; i++)  
                        Member men = new Member();  
                        men.Age = radom.Next(100).ToString();  
                        men.Name = "JOE" + i.ToString();  
                        men.Email = new Uri("mailto:JOE" + i.ToString() + "");  
                    dataGrid.DataContext = memberData;  
            public class Member  
                public string Name { get; set; }  
                public string Age { get; set; }  
                public Uri Email { get; set; }  

    Step 4: Code for Form1.cs:

     public partial class Form1 : Form  
            private ElementHost _elemHost = new ElementHost();  
            private WPFControls _WPFDataGrid = new WPFControls();   
            public Form1()  
                _elemHost.Location = new Point(10, 10);  
                _elemHost.Child = _WPFDataGrid;   
                _elemHost.Width = 400;  
                _elemHost.Height = 300;  

    Step 5: Run and get the result picture:

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Most helpful