SindhuH-2474 avatar image
0 Votes"
SindhuH-2474 asked HuiLiu-MSFT answered

Creating a WPF Custom Control in Windows Form but with Error message : Make sure the type has a default constructor

I am creating a custom WPF control in windows form for a temperature dial in When I try to add the WPF control in windows form using ElementHost, I Get the error message "Make sure the type has a default constructor135832-picture1.png". And yes I have added WindowsFormsIntegration.dll to my project's references.

I have attached the code and WPF control Image.

Please help me to resolve the issue.

Thank you in advance

dial.png (188.7 KiB)
picture1.png (145.1 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The error message - Make sure the type has a default constructor

0 Votes 0 ·

Hi,@ SindhuH-2474. What is the Target framework of your Winform project and wpf custom control? Did you refer to the steps here to add wpf control in winform? And please show more steps and codes to analyze the problem.

0 Votes 0 ·

Hello @HuiLiu-MSFT,

I extend my apologies for my late reply and am also thankful for your patience.
My Target Framework is 4.7.2. Yes, with the basic steps of adding customized WPF control, I followed the following video link, since I had to design a dial gauge for my project.
And I am using VB language to write my code.

Thank you

0 Votes 0 ·
Show more comments

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered

I followed the link you sent and used the steps and code below to run the program successfully.
1. Create a new Windows Forms project.
2. Add the WPF User Control Library project to the solution.
3. Create a new WPF user control to the WPF User Control Library project.
4. Add a reference to the WPF User Control Library from the Windows Forms project.
5. Also add a reference to the WindowsFormsIntegration.dll and PresentationCore.dll assemblies from the Windows Forms project.
6. Create an ElementHost object and an instance of the UserControl in the Windows Forms application and set the Child property of the ElementHost control to the UserControl instance. Finally, add the ElementHost object to the Controls collection of the form.

The code of UserControl.xaml (WPF User Control Library project):

 <UserControl x:Class="UserControl1"
              d:DesignHeight="450" d:DesignWidth="450">
             <Ellipse Name="gauge" HorizontalAlignment="Left" Height="400"  StrokeThickness="10" Margin="23,25,0,0" Stroke="Black" VerticalAlignment="Top" Width="400">
                         <GradientStop Color="Black" Offset="1"/>
                         <GradientStop Color="White"/>
             <Ellipse HorizontalAlignment="Left" Height="49" StrokeThickness="0" Margin="205,205,0,0" Stroke="Black" VerticalAlignment="Top" Width="45" RenderTransformOrigin="0.274,0.252">
                         <GradientStop Color="Black" Offset="1"/>
                         <GradientStop Color="White"/>
             <Rectangle Name="Needle" HorizontalAlignment="Left" Height="188" Margin="220,71,0,0"  VerticalAlignment="Top" Width="13" >
                     <ImageBrush >
                             <BitmapImage UriSource="rr.jpg"/>

The code of UserControl.xaml.vb:

 Imports System.Windows.Media
 Partial Public Class UserControl1
     Inherits System.Windows.Controls.UserControl
     Public pointerValue As Double
     Public Sub New()
     End Sub
     Public Sub changevalue()
         Dim rotate As RotateTransform = New RotateTransform(pointerValue)
         Needle.RenderTransform = rotate
     End Sub
 End Class

Add a reference of WpfControlLibrary1 to WindowsApp1:
The designer of Form1.vb:
The code of Form1.vb:

 Imports System.Windows.Forms.Integration
 Imports WpfControlLibrary1
 Public Class Form1
     Private gauge As UserControl1
     Private host As ElementHost
     Public Sub New()
         gauge = New UserControl1()
         host = New ElementHost()
         host.Dock = DockStyle.Fill
         host.Child = gauge
     End Sub
     Private value As Integer
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         Integer.TryParse(valuepointer.Text, value)
         gauge.pointerValue = value
     End Sub
 End Class

The picture of result:

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. 

image.png (20.5 KiB)
image.png (12.0 KiB)
image.png (96.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.