Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf WindowsFormsHost控件不遵循构造函数中设置的大小_Wpf_Windowsformshost - Fatal编程技术网

Wpf WindowsFormsHost控件不遵循构造函数中设置的大小

Wpf WindowsFormsHost控件不遵循构造函数中设置的大小,wpf,windowsformshost,Wpf,Windowsformshost,我有一个windows窗体用户控件: partial class LOLControl { private System.ComponentModel.IContainer components = null; #region Component Designer generated code private void InitializeComponent() { this.button1 = new System.Windows.For

我有一个windows窗体用户控件:

partial class LOLControl
{

    private System.ComponentModel.IContainer components = null;

    #region Component Designer generated code


    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();

        this.button1.Location = new System.Drawing.Point(436, 472);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 1;
        this.button1.Text = "hey";
        this.button1.UseVisualStyleBackColor = true;

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.SystemColors.HotTrack;
        this.Controls.Add(this.button1);
        this.Name = "LOLControl";
        this.Size = new System.Drawing.Size(518, 509);
        this.ResumeLayout(false);

    }


    private System.Windows.Forms.Button button1;
}
它通过WindowsFormHost托管在WPF窗口中:

<Window x:Class="Production.WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Production.WPF"
        mc:Ignorable="d"
        Title="MainWindow" ResizeMode="NoResize">

        <WindowsFormsHost>
            <local:LOLControl x:Name="mapControlWrapper"/>
        </WindowsFormsHost>
</Window>

请注意,
LOLControl
的大小是
(518509)
——这是我在使用Windows窗体设计器调整控件大小时感到满意的神奇数字。我希望我的WPF窗口也有这个大小

我假设如果
LOLControl
大小是
(518509)
,那么在运行时,我将看到
窗口的大小与我在设计时看到的
LOLControl
大小相同

我检查在WPF
窗口
构造函数中初始化
LOLControl
时,
LOLControl
的大小是否仍与
LOLControl.InitializedComponent
方法中的设置一致(即:
(518509)

然而,在编译和运行时,结果是
LOLControl
大小与设计时不同——运行时窗口的大小比设计时大


为什么?

您是在抱怨WPF窗口的大小还是宿主控件的大小?因为运行时行为才是真正重要的,为什么不干脆增加WindowsFormsHost控件的大小?您可以像使用任何其他控件一样为其指定宽度和高度。@mm8,您的意思是什么?现在的问题是,我指定的代码中的宽度和高度在运行时不受尊重。增加windowsformhost控件的大小如何解决此问题?不知道为什么,看起来它拾取了存储在注册表中的固定大小的值。解决方法是设置窗口的
SizeToContent
属性。