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">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
Step 2: Add a WPF UserControl named WPFControls
Step 3: Code for WPFControls.xaml
<Grid>
<DataGrid x:Name="dataGrid" ItemsSource="{Binding }" AutoGenerateColumns="False" SelectionMode="Extended" SelectionUnit="CellOrRowHeader" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="80" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Age" Width="50" Binding="{Binding Age}" />
<DataGridHyperlinkColumn Header="Email" Width="150" Binding="{Binding Email}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
WPFControls.cs
public partial class WPFControls : UserControl
{
public WPFControls()
{
InitializeComponent();
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() + "+@school.com");
memberData.Add(men);
}
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()
{
InitializeComponent();
_elemHost.Location = new Point(10, 10);
_elemHost.Child = _WPFDataGrid;
_elemHost.Width = 400;
_elemHost.Height = 300;
this.Controls.Add(_elemHost);
}
}
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.