Window 未根据内容调整大小
Window not Resizing to content
我有一个window:
<Window x:Class="HarryPotter.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:HarryPotter"
mc:Ignorable="d"
Title="Harry Potter" SizeToContent="WidthAndHeight" Background="Black" WindowStartupLocation="CenterScreen"
>
<Viewbox Stretch="Uniform">
<Canvas Height="640" Width="480" Background="DodgerBlue">
</Canvas>
</Viewbox>
</Window>
在设计器中,ViewBox的Width = 480,Height = 640。所以Window的Width和Height分别是488和671,这正是我想要的。
我希望 ViewBox 在启动时与 Canvas 大小相同,并且 window 大小与 ViewBox 的大小相同。然后我希望 window 可以自由调整大小。然而,事实并非如此。当 window 打开时,它的高度就是我显示器的高度。
我该如何解决这个问题?
您正在使用 ViewBox
通过均匀拉伸使内容适合可用的 space。但是,由于您没有为可用的 space (SizeToContent = "WidthAndHeight"
) 定义高度或宽度,它会占用所有 space,以便它可以拉伸您的 canvas.
要进行实验,您可以设置 Height="50"
并查看 window 占用所有水平 space。
要防止它,您有几种选择:
- 将
ViewBox
的 Stretch
属性 设置为 None
而不是 Uniform
。
- 为您的
Window
设置预定义的Width
或Height
。
- 设置
MaxHeight
或MaxWidth
属性共ViewBox
。
我有一个window:
<Window x:Class="HarryPotter.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:HarryPotter"
mc:Ignorable="d"
Title="Harry Potter" SizeToContent="WidthAndHeight" Background="Black" WindowStartupLocation="CenterScreen"
>
<Viewbox Stretch="Uniform">
<Canvas Height="640" Width="480" Background="DodgerBlue">
</Canvas>
</Viewbox>
</Window>
在设计器中,ViewBox的Width = 480,Height = 640。所以Window的Width和Height分别是488和671,这正是我想要的。
我希望 ViewBox 在启动时与 Canvas 大小相同,并且 window 大小与 ViewBox 的大小相同。然后我希望 window 可以自由调整大小。然而,事实并非如此。当 window 打开时,它的高度就是我显示器的高度。
我该如何解决这个问题?
您正在使用 ViewBox
通过均匀拉伸使内容适合可用的 space。但是,由于您没有为可用的 space (SizeToContent = "WidthAndHeight"
) 定义高度或宽度,它会占用所有 space,以便它可以拉伸您的 canvas.
要进行实验,您可以设置 Height="50"
并查看 window 占用所有水平 space。
要防止它,您有几种选择:
- 将
ViewBox
的Stretch
属性 设置为None
而不是Uniform
。 - 为您的
Window
设置预定义的Width
或Height
。 - 设置
MaxHeight
或MaxWidth
属性共ViewBox
。