Unwanted Space and Margins are surrounding the control in designer when Integrating WPF User-Control into Form using ElementHost

bagavathi basker 0 Reputation points
2023-06-21T06:32:40.6233333+00:00

When integrating the WPF user-control (For Example: The UserControl contains the button) into a form using ElementHost, there are unwanted spaces and margins surrounding the control in the designer. It works fine in runtime; reported issue is reproduced only in design time. How can I remove the space and margins surrounding the control in the form's designer.

It works fine in runtime; reported issue is reproduced only in design time.

private void InitializeComponent()         
{             
this.elementHost1 = new System.Windows.Forms.Integration.ElementHost();             this.userControl11 = new WindowsFormsApp1.UserControl1();             this.SuspendLayout();             
this.elementHost1.Location = new System.Drawing.Point(146, 95);             this.elementHost1.Name = "elementHost1";             this.elementHost1.Size = new System.Drawing.Size(200, 100);             this.elementHost1.TabIndex = 0;             
this.elementHost1.Text = "elementHost1";             this.elementHost1.Child = this.userControl11;             this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;             this.ClientSize = new System.Drawing.Size(800, 450);             this.Controls.Add(this.elementHost1);             
this.Name = "Form1";             
this.Text = "Form1";             
this.ResumeLayout(false);          
} 

WinFormDesignerIssue

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 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,784 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2023-06-21T07:19:50.78+00:00

    Hi,@ bagavathi basker.Welcome Microsoft Q&A.

    You could add controls ElementHost and UserControl1 in Form.cs.

     public partial class Form1 : Form
        {
            ElementHost elementHost1;
            UserControl1 userControl11;
            public Form1()
            {
                InitializeComponent();
    
                this.elementHost1 = new System.Windows.Forms.Integration.ElementHost();
                this.userControl11 = new UserControl1();
                this.SuspendLayout();
                this.elementHost1.Location = new System.Drawing.Point(146, 95);
                this.elementHost1.Name = "elementHost1";
                this.elementHost1.Size = new System.Drawing.Size(200, 100);
                this.elementHost1.TabIndex = 0;
                this.elementHost1.Text = "elementHost1";
                this.elementHost1.Child = this.userControl11;
                this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(800, 450);
                this.Controls.Add(this.elementHost1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
            }
        }
    

    UserControl1:

    <StackPanel>
            <TextBlock Text="hello" Width="100" Height="40" Background="AliceBlue"/>
            <Button Content="hello" Width="100" Height="40" Background="AliceBlue"/>
        </StackPanel>
    

    The result:

    User's image


    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.


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.