在 C# 中以编程方式调整 wpf 窗口大小
我有一个 wpf 窗口,其中有两个用户控件,其中第二个仅在需要时显示。我仅在 XAML 中设置窗口的 MinWidth,MinHeight 是通过数据绑定提供的,根据是否显示第二个用户控件设置。现在:如何在运行时将窗口的大小设置为与 MinWidth/Height 不同的值。我尝试在 Show() 之前、Show() 之后、在各种事件(初始化、加载等)中设置值。我尝试过使用和不使用 UpdateLayout(),我尝试通过数据绑定设置高度/宽度。什么都不起作用!但是,当我调试这些方法时,我发现窗口的高度/宽度属性设置为预期值,但实际高度/宽度保持不变。我以为这会是一件小事,但事实证明它不是(对我来说)。希望您能提供帮助。
I have a wpf window with two usercontrols inside of which the second is only shown when needed. I only set the MinWidth of the window in XAML, the MinHeight is provided through databinding an ist set depending on if the second usercontrol is shown. Now: How can I set the size of the window to different values than the MinWidth/Height during runtime. I tried setting the values before the Show(), after the Show(), in various events (Initialized, Loaded, etc.). I tried with and without UpdateLayout(), I tried setting the Height/Width through databinding. Nothing works! But when I debug the approaches I see that the Height/Width properties of the window are set to the expected values, but ActualHeight/Width stay. I thought it would be a bagatelle but it turned out it is not (to me). Thy for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过设置
简短的添加:我刚刚在代码中做了一个简短的测试:
并且XAML:
单击按钮设置窗口高度。这就是您要找的吗?
Have you tried to set
Short addition: I just did a short test, in code:
and the XAML:
Click on the Button sets the window height. Is that what you´re looking for?
你没有提供太多信息,所以我只是在这里猜测。
我可以重现窗口而不考虑其宽度和高度设置的唯一方法是将
SizeToContent
设置为WidthAndHeight
。You have no given much information, so I am just guessing here.
The only way I could reproduce the window not respecting its width and height settings was when I set
SizeToContent
toWidthAndHeight
.如果有人仍在为此苦苦挣扎,您唯一要做的就是以下内容:
如果您想调整主窗口的大小,只需编写以下代码。
如果您想调整主窗口以外的新窗口的大小,只需在新窗口的 .cs 文件中编写以下代码即可。
希望有帮助。
if someone is still struggling with this, the only thing you have to do is the following:
If you want to resize the main window just write the following code.
If you want to resize a new window other than the main window just write the following code in the .cs file of the new window.
Hope it helps.
我只是简单地设置窗口高度属性。
最初
_myWindow.Height = 400;
一旦内容需要调整大小,我将其设置如下:
double newWindowHeight = a + b;
其中 a + b 使窗口变大,并且所以:_myWindow.Height = newWindowHeight;
I just simply set the window Height property.
Originally
_myWindow.Height = 400;
And once the content required resizing I set it as follows:
double newWindowHeight = a + b;
Where a + b make the window larger, and so:_myWindow.Height = newWindowHeight;