我想设置WPF窗口的初始客户端大小。我看不到一种简单的方法来做这件事。
具体地说,当我的窗口打开时,我希望它的大小足以让它的内容在不需要滚动条的情况下适合它。但在显示之后,我希望窗口可以自由调整大小(可以更大,也可以更小)。
如果我在Window元素上设置Width和Height属性,就会设置非客户端(外部)大小,这是没有用的。一旦标题栏和调整大小的边框蚕食了这个空间,客户区将不再足够大来容纳它的内容,我将有滚动条。我可以通过选择更大的尺寸来补偿,但标题栏高度和边框粗细都是用户可自定义的(以及默认设置因操作系统版本而异),在不同的机器上不一定相同。
我可以在窗口的内容元素(本例中为<Grid>
)上设置宽度和高度,然后将窗口的SizeToContent属性设置为WidthAndHeight。这将使窗口的初始大小正好达到我想要的大小。但是事情不再调整大小--我可以调整窗口的大小,但它的内容不会随之调整大小,因为我指定了一个固定的大小。
有没有办法设置窗口的初始客户端大小,最好不要代码隐藏?
发布于 2009-07-15 23:04:51
您可以在Load事件处理程序的代码隐藏中使用以下两种方法之一来完成此操作:
注意:在这两个示例中,LayoutRoot网格的内容是相同的,但是LayoutRoot上的宽度和高度仅在示例A中指定。
A)在窗口的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);
}
}
}
假设页面布局如下(请注意窗口上的SizeToContent设置和已加载的事件处理程序以及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)考虑到系统特定的客户端窗口框架大小来设置窗口的宽度和高度:
使用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;
}
}
}
假设页面布局成比例(请注意,在窗口上没有SizeToContent设置,也没有在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中以声明方式完成这项工作的方法。
发布于 2009-07-04 05:44:56
您可以删除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>
开始时,它看起来是这样的:
不过,您仍然可以使用鼠标将其拉伸:
发布于 2009-07-04 12:29:01
我在构造函数中执行以下操作,并在xaml中添加ResizeMode="CanResizeWithGrip“,但这在某种程度上取决于内容在启动时占用了多少空间
public Window1()
{
this.Height = SystemParameters.WorkArea.Height;
this.Width = SystemParameters.WorkArea.Width;
}
https://stackoverflow.com/questions/1081580
复制相似问题