如何设置WPF窗口的启动ClientSize?

Joe*_*ite 17 size wpf

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

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

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

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

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

Tim*_*son 20

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

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

A)Window的SizeToContent上的ClearValue以及内容的宽度和高度:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

假设页面布局类似(注意Window上的SizeToContent设置和Loaded事件处理程序以及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>
Run Code Online (Sandbox Code Playgroud)

要么

B)为系统特定的客户端窗口框架大小设置Window的宽度和高度;

使用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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

假设比例页面布局像(注意窗口上没有SizeToContent设置或Loaded事件处理程序,或者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>
Run Code Online (Sandbox Code Playgroud)

我还没有想出办法在XAML中以声明的方式做到这一点.


Ore*_*ner 13

您可以删除XAML中的窗口宽度和高度属性,并添加SizeToContent ="WidthAndHeight".这会将窗口的初始尺寸设置为其内容,但仍允许您调整窗口大小.

<Window x:Class="WpfApplication2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" SizeToContent="WidthAndHeight">
    <Grid>
        <TextBlock Text="How to set WPF window’s startup ClientSize?"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

启动时,它看起来像这样:

alt text http://img7.imageshack.us/img7/1285/onstart.png

然而你仍然可以用鼠标拉伸它:

alt text http://img16.imageshack.us/img16/6687/stretched.png


Sim*_*ier 6

我花了很长时间才弄清楚整个故事.在网上找到这个问题的纯XAML(零代码)答案是非常困难的,所以这是我的.

当我们在Visual Studio WPF设计器中设计一个Window时,标准(默认情况下)的方式是在XAML中定义Width和定义Height属性Window,如下所示:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="75" Width="190">
    <Grid>
        <Button Content="Right" HorizontalAlignment="Right" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="75" />
        <Button Content="Left" HorizontalAlignment="Left" Margin="10,0,0,10" VerticalAlignment="Bottom" Width="75"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

设计器预览如下所示:

在此输入图像描述

一切看起来很酷,但是当我们运行应用程序,这取决于当前的Windows版本,主题和所有的显示设置的爵士,也有99%的机会,该运行系统窗口将不会看起来像一个设计.以下是我在Windows 8.1上看到的内容:

在此输入图像描述

最初的解决方案就像Oren对使用该SizeTocontent属性的回答一样.它基本上告诉WPF从它的内容(又名"客户端大小")定义窗口大小,而不是窗口本身(也就是"客户端大小+所有非客户端/ chrome /边界完全无法控制的东西").

因此,我们可以将内容定义为固定大小,如下所示:

<Window x:Class="WpfApplication1.MainWindowSizeToContentCentered"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="WidthAndHeight">
    <Grid Height="40" Width="180">
        <Button Content="Right" HorizontalAlignment="Right" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="75" />
        <Button Content="Left" HorizontalAlignment="Left" Margin="10,0,0,10" VerticalAlignment="Bottom" Width="75"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

现在,运行时窗口看起来正是我们想要的(注意网格的高度和宽度与原始窗口的值不完全相同 - 40/180 vs 75/190 - ,这很好,就像在设计模式中一样,你现在可以永远忘记窗口高度和宽度属性):

在此输入图像描述

当窗口调整大小时,它的表现如何?像这样,网格是居中的,如果您想要这种行为,这很好:

在此输入图像描述

但是,如果我们想要问题中的行为,("我可以调整窗口大小,但其内容不会随之调整大小,因为我指定了固定大小."),这也是原始行为,而不是指定宽度和高度,我们可以使用MinWidthMinHeight,如下所示:

<Window x:Class="WpfApplication1.MainWindowSizeToContentResizable"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="WidthAndHeight">
    <Grid MinHeight="40" MinWidth="180">
        <Button Content="Right" HorizontalAlignment="Right" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="75" />
        <Button Content="Left" HorizontalAlignment="Left" Margin="10,0,0,10" VerticalAlignment="Bottom" Width="75"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

运行时窗口看起来相同,但调整大小体验现在与原始默认窗口布局相当:

在此输入图像描述

这应该是默认的WPF设计器布局恕我直言.