Wpf WindowsFormsHost控件不遵循构造函数中设置的大小
我有一个windows窗体用户控件: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
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
属性。