窗口高度=“自动"没有按预期工作 [英] Window Height="Auto" not working as expected

查看:20
本文介绍了窗口高度=“自动"没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是显示一个没有明确具有高度/宽度的窗口(两个值都省略或设置为Auto).我猜测窗口会通过自动计算所有包含的用户控件大小来找出它的大小,但这实际上不起作用!

What I am trying to do is show a window, that does not explicitly have a height/width, (both values omitted or set to Auto). I was guessing that the window would find out its size by auto - calculating all contained usercontrols sizes, but this doesn't actually work!

相反,我得到一个大窗口,ActualwidthActualheight 值都设置为 512 (?!?!)

Instead I get a big window with Actualwidth and Actualheight values both set to 512 (?!?!)

窗口声明:

<Window x:Class="Window3"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window3" 
  Height="Auto">
<StackPanel>
    <Label>Window</Label>
</StackPanel>
</Window>

通过以下方式将此窗口显示为对话框:

Showing this window as a dialog via:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
    Dim dlg As New Window3
    dlg.ShowDialog()
End Sub

有没有办法解决这个问题?我不想显式设置我的窗口的大小,因为表单中的许多控件将根据构造函数参数折叠,并且试图找到表单的实际大小会很棘手(而且很难看).

Is there a solution for this? I don't want to explicitly set the size of my window because many controls in the form will be collapsed based on constructor parameters, and trying to find the actual size of the form would be tricky (and ugly).

推荐答案

设置窗口的属性 SizeToContent="WidthAndHeight".这应该会有所帮助.

Set the window's property SizeToContent="WidthAndHeight". This should help.

这篇关于窗口高度=“自动"没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆