如何设置 WPF 窗口的启动 ClientSize? [英] How to set WPF window's startup ClientSize?

查看:13
本文介绍了如何设置 WPF 窗口的启动 ClientSize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置我的 WPF 窗口的初始 客户端 大小.我没有看到一种直接的方法来做到这一点.

I want to set my WPF window's initial client size. I'm not seeing a straightforward way to do this.

具体来说,当我的窗口打开时,我希望它的大小刚好足以容纳其内容而无需滚动条.但是在显示之后,我希望窗口可以自由调整大小(更大或更小).

Specifically, when my window opens, I want it to be sized just big enough for its contents to fit without needing scrollbars. But after it's shown, I want the window to be freely resizable (either larger or smaller).

如果我在我的 Window 元素上设置 Width 和 Height 属性,则会设置非客户端(外部)大小,这没有用.一旦标题栏和调整边框占据该空间,客户区将不再足够容纳其内容,我将拥有滚动条.我可以通过选择更大的尺寸来补偿,但标题栏高度和边框厚度都是用户可自定义的(以及默认值因操作系统版本而异),并且在不同的机器上不一定相同.

If I set Width and Height attributes on my Window element, that sets the non-client (outer) size, which isn't useful. Once the titlebar and resize borders eat into that space, the client area will no longer be big enough for its content, and I'll have scrollbars. I could compensate by picking a larger size, but both titlebar height and border thickness are user-customizable (as well as the defaults varying by OS version) and won't necessarily be the same on a different machine.

我可以在窗口的内容元素(本例中为 )上设置 Width 和 Height,然后将 Window 的 SizeToContent 属性设置为 WidthAndHeight.这使窗口的初始大小正好在我想要的位置.但后来事情不再调整大小——我可以调整窗口大小,但它的内容不会随之调整大小,因为我指定了一个固定大小.

I can set Width and Height on the window's content element (a <Grid> in this case), and then set the Window's SizeToContent attribute to WidthAndHeight. That gets the window's initial size exactly where I want it. But then things don't resize anymore -- I can resize the window, but its content doesn't resize with it, because I specified a fixed size.

有没有办法设置窗口的初始客户端大小,最好没有代码隐藏?(如果这是唯一的方法,我会采用代码隐藏,但如果有人有这种方法,我更喜欢仅 XAML 的方法.)

Is there any way to set a Window's initial client size, preferably without code-behind? (I'll take code-behind if that's the only way, but I'd prefer a XAML-only approach if anyone has one.)

推荐答案

您可以通过以下两种方式之一在 Load 事件处理程序的代码隐藏中执行此操作:

You can do it in code-behind on the Load event handler in one of two ways:

注意:两个示例中 LayoutRoot Grid 的内容相同,但 LayoutRoot 上的 Width 和 Height 仅在示例 A 中指定.

NOTE: The content of the LayoutRoot Grid is the same in both examples, but the Width and Height on the LayoutRoot are only specified in example A.

A) 窗口的 SizeToContent 和内容的宽度和高度上的 ClearValue:

A) ClearValue on the the Window's SizeToContent and on the content's Width and Height:

using System.Windows;

namespace WpfWindowBorderTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ClearValue(SizeToContentProperty);
            LayoutRoot.ClearValue(WidthProperty);
            LayoutRoot.ClearValue(HeightProperty);
        }
    }
}

假设页面布局如下(注意 Window 上的 SizeToContent 设置和 Loaded 事件处理程序以及 LayoutRoot 上指定的 Width 和 Height):

assuming a page layout like (note the SizeToContent setting and Loaded event handler on the Window and the Width and Height specified on the LayoutRoot):

<Window x:Class="WpfWindowBorderTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" SizeToContent="WidthAndHeight" Loaded="Window_Loaded">
    <Grid x:Name="LayoutRoot" Width="300" Height="300" Background="Green">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="3*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <Rectangle Grid.Row="0" Grid.Column="0" Fill="Black" />
        <Rectangle Grid.Row="0" Grid.Column="0" Width="75" Height="75" Fill="Red" />
        <Rectangle Grid.Row="1" Grid.Column="1" Fill="Yellow" />
        <Rectangle Grid.Row="1" Grid.Column="1" Width="225" Height="225" Fill="Red" />
    </Grid>
</Window>

B) 根据系统特定的客户端窗口框架大小设置窗口的宽度和高度:

B) setting the Window's Width and Height accounting for the System-specific client window frame sizes:

使用 System.Windows;

using System.Windows;

namespace WpfWindowBorderTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            const int snugContentWidth = 300;
            const int snugContentHeight = 300;

            var horizontalBorderHeight = SystemParameters.ResizeFrameHorizontalBorderHeight;
            var verticalBorderWidth = SystemParameters.ResizeFrameVerticalBorderWidth;
            var captionHeight = SystemParameters.CaptionHeight;

            Width = snugContentWidth + 2 * verticalBorderWidth;
            Height = snugContentHeight + captionHeight + 2 * horizontalBorderHeight;
        }
    }
}

假设页面布局成比例(注意在 Window 上没有 SizeToContent 设置或 Loaded 事件处理程序或在 LayoutRoot 上指定的宽度和高度):

assuming a proportional page layout like (note no SizeToContent setting or Loaded event handler on the Window or Width and Height specified on the LayoutRoot):

<Window x:Class="WpfWindowBorderTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1">
    <Grid x:Name="LayoutRoot" Background="Green">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="3*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <Rectangle Grid.Row="0" Grid.Column="0" Fill="Black" />
        <Rectangle Grid.Row="0" Grid.Column="0" Width="75" Height="75" Fill="Red" />
        <Rectangle Grid.Row="1" Grid.Column="1" Fill="Yellow" />
        <Rectangle Grid.Row="1" Grid.Column="1" Width="225" Height="225" Fill="Red" />
    </Grid>
</Window>

我还没有想出一种在 XAML 中以声明方式执行此操作的方法.

I haven't been able to come up with a way to do it declaratively in XAML as yet.

这篇关于如何设置 WPF 窗口的启动 ClientSize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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