question

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 vb.net. 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


windows-wpfwindows-forms
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.

https://www.youtube.com/watch?v=k6rYqXu5sDk



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"
              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" 
              xmlns:local="clr-namespace:WpfControlLibrary1"
              mc:Ignorable="d" 
              d:DesignHeight="450" d:DesignWidth="450">
     <Viewbox>
         <Grid>
             <Ellipse Name="gauge" HorizontalAlignment="Left" Height="400"  StrokeThickness="10" Margin="23,25,0,0" Stroke="Black" VerticalAlignment="Top" Width="400">
                 <Ellipse.Fill>
                     <RadialGradientBrush>
                         <GradientStop Color="Black" Offset="1"/>
                         <GradientStop Color="White"/>
                     </RadialGradientBrush>
                 </Ellipse.Fill>
             </Ellipse>
             <Ellipse HorizontalAlignment="Left" Height="49" StrokeThickness="0" Margin="205,205,0,0" Stroke="Black" VerticalAlignment="Top" Width="45" RenderTransformOrigin="0.274,0.252">
                 <Ellipse.Fill>
                     <RadialGradientBrush>
                         <GradientStop Color="Black" Offset="1"/>
                         <GradientStop Color="White"/>
                     </RadialGradientBrush>
                 </Ellipse.Fill>
             </Ellipse>
             <Rectangle Name="Needle" HorizontalAlignment="Left" Height="188" Margin="220,71,0,0"  VerticalAlignment="Top" Width="13" >
                 <Rectangle.Fill>
                     <ImageBrush >
                         <ImageBrush.ImageSource>
                             <BitmapImage UriSource="rr.jpg"/>
                         </ImageBrush.ImageSource>
                     </ImageBrush>
                 </Rectangle.Fill>
             </Rectangle>
    
         </Grid>
     </Viewbox>
 </UserControl>

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()
         InitializeComponent()
     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:
138016-image.png
The designer of Form1.vb:
138015-image.png
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()
         InitializeComponent()
         gauge = New UserControl1()
         host = New ElementHost()
         host.Dock = DockStyle.Fill
         host.Child = gauge
         Me.Controls.Add(host)
    
     End Sub
     Private value As Integer
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         gauge.changevalue()
         Integer.TryParse(valuepointer.Text, value)
         gauge.pointerValue = value
     End Sub
 End Class

The picture of result:
137970-image.png


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.