创建WPF项目并在下面运行代码时,我将在下面获取窗口。我很困惑,因为椭圆直径和窗户宽度/高度都是" 200",但椭圆不适合窗户。
另外,我没有发布图像,但是我试图将窗口的高度和宽度设置为上一个实验中的96,而高度小于宽度。纵横比有点像我的显示的长宽比。
我在WPF中读到,每个单元为1/96英寸。我认为这对于画布中的椭圆都是如此。然后,窗口大小呢?它使用什么单位?
图像:
简单的WPF应用程序窗口图像。
代码:
<Window x:Class="clock2.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:clock2"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="200">
<Grid>
<Canvas ClipToBounds="True">
<Ellipse Canvas.Left="0.0" Canvas.Top="0.0" Width="200.0" Height="200.0" Fill="lightgray" />
</Canvas>
</Grid>
</Window>
窗口的宽度和高度包括边框和标题栏的尺寸。
设置网格的大小,然后在窗口上设置SizeToContent="WidthAndHeight"
:
<Window ... SizeToContent="WidthAndHeight">
<Grid Height="200" Width="200">
<Canvas ClipToBounds="True">
<Ellipse Canvas.Left="0.0" Canvas.Top="0.0"
Width="200.0" Height="200.0" Fill="LightGray" />
</Canvas>
</Grid>
</Window>
如果设置了画布的大小并使用SizeToContent="WidthAndHeight"
,则可以摆脱网格:
<Window ... SizeToContent="WidthAndHeight">
<Canvas Width="200" Height="200" Background="Aqua">
<Ellipse Width="200" Height="200" Fill="red" Canvas.Top="0" Canvas.Left="0"/>
</Canvas>
</Window>