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

Sindhu H 201 Reputation points
2021-09-28T08:14:08.68+00:00

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

Developer technologies Windows Forms
Developer technologies Windows Presentation Foundation
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2021-10-06T06:47:01.287+00:00

    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][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.