【问题标题】:WPF units on window and canvas are different. What is window size unit?窗口和画布上的 WPF 单位不同。什么是窗口大小单位?
【发布时间】:2016-11-22 18:22:01
【问题描述】:

当我创建一个 WPF 项目并运行下面的代码时,我会看到下面的窗口。我很困惑,因为椭圆直径和窗口宽度/高度都是“200”,但椭圆不适合窗口。

另外,我不是发图片,而是在之前的实验中尝试将窗口的高度和宽度设置为96,高度小于宽度。纵横比有点像我的显示器的纵横比。

我读到在 WPF 中每个单位是 1/96 英寸。我认为这对于画布中的椭圆是正确的。那么,窗口大小呢?它使用什么单位?

图片:

Simple WPF application window image.

代码:

<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>

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    窗口的宽度和高度包括边框和标题栏的尺寸。

    改为设置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>
    

    【讨论】:

      猜你喜欢
      • 2017-04-08
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多