WindowsFormsHost控件不遵循构造函数中设置的Size

时间:2017-06-20 09:59:47

标签: 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.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的大小为(518, 509) - 这是我在使用Windows窗体设计器调整控件大小时感到满意的神奇数字。我希望我的WPF窗口也具有这个大小。

我认为如果LOLControl大小为(518, 509),那么在运行时,我会看到Window的大小与我看到的LOLControl大小相同在设计时。

我检查一下,当我在WPF LOLControl构造函数中初始化Window时,LOLControl的大小仍然是LOLControl.InitializedComponent方法中的设置(即:(518, 509))。

然而,在编译和运行时,事实证明LOLControl大小与设计时间相同 - 运行时窗口大小大于设计时间!

为什么?

0 个答案:

没有答案