如何设置 WPF 窗口的启动 ClientSize?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1081580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set WPF window's startup ClientSize?
提问by Joe White
I want to set my WPF window's initial clientsize. I'm not seeing a straightforward way to do this.
我想设置我的 WPF 窗口的初始客户端大小。我没有看到一种直接的方法来做到这一点。
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).
具体来说,当我的窗口打开时,我希望它的大小刚好足以容纳其内容而无需滚动条。但是在显示之后,我希望窗口可以自由调整大小(更大或更小)。
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.
如果我在 Window 元素上设置 Width 和 Height 属性,则会设置非客户端(外部)大小,这没有用。一旦标题栏和调整边框占据该空间,客户区将不再足够容纳其内容,我将拥有滚动条。我可以通过选择更大的尺寸来补偿,但标题栏高度和边框厚度都是用户可定制的(以及默认值因操作系统版本而异),并且在不同的机器上不一定相同。
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.
我可以在窗口的内容元素(<Grid>
本例中为 a)上设置 Width 和 Height ,然后将 Window 的 SizeToContent 属性设置为 WidthAndHeight。这使窗口的初始大小正好在我想要的位置。但是后来事情不再调整大小了——我可以调整窗口的大小,但它的内容不会随之调整大小,因为我指定了一个固定的大小。
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.)
有没有办法设置 Window 的初始客户端大小,最好没有代码隐藏?(如果这是唯一的方法,我会采用代码隐藏,但如果有人有 XAML 方法,我更喜欢只使用 XAML 的方法。)
回答by Tim Erickson
You can do it in code-behind on the Load event handler in one of two ways:
您可以通过以下两种方式之一在 Load 事件处理程序的代码隐藏中执行此操作:
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.
注意:两个示例中 LayoutRoot Grid 的内容相同,但 LayoutRoot 上的 Width 和 Height 仅在示例 A 中指定。
A) ClearValue on the the Window's SizeToContent and on the content's Width and Height:
A) Window 的 SizeToContent 和内容的 Width 和 Height 上的 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);
}
}
}
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 上的 SizeToContent 设置和 Loaded 事件处理程序以及 LayoutRoot 上指定的 Width 和 Height):
<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>
or
或者
B) setting the Window's Width and Height accounting for the System-specific client window frame sizes:
B) 根据系统特定的客户端窗口框架大小设置窗口的宽度和高度:
using System.Windows;
使用 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;
}
}
}
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):
假设页面布局成比例(注意窗口上没有 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>
I haven't been able to come up with a way to do it declaratively in XAML as yet.
到目前为止,我还无法想出一种在 XAML 中以声明方式执行此操作的方法。
回答by Oren Trutner
You can remove the window Width and Height attributes in XAML, and add SizeToContent="WidthAndHeight". This sets the initial dimensions of the window to its content, yet still lets you resize it.
您可以在 XAML 中删除窗口 Width 和 Height 属性,并添加 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>
When started, it looks like this:
启动时,它看起来像这样:
Yet you can still stretch it with the mouse:
但是你仍然可以用鼠标拉伸它:
回答by Simon Mourier
I spend quite a time to figure that whole story too. It's surprisingly difficult to find a pure XAML (zero-code behind) answer to this question on the net, so here's mine.
我也花了很多时间来弄清楚整个故事。在网上找到这个问题的纯 XAML(零代码隐藏)答案非常困难,所以这是我的。
When we design a Window in the Visual Studio WPF designer, the standard (and by default) way is to define Width
and Height
properties on the Window
, like this in XAML:
当我们在 Visual Studio WPF 设计器中设计 Window 时,标准(默认情况下)方法是在 上定义Width
和Height
属性Window
,就像在 XAML 中这样:
<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>
The designer preview looks like this:
设计器预览如下所示:
Everything looks cool, but when we run the application, depending on the current Windows version, theme and all display settings jazz, there are 99% chances that the runtime window will notlook like the designed one. Here is what it looks on my Windows 8.1:
一切看起来很酷,但是当我们运行应用程序,这取决于当前的Windows版本,主题和所有的显示设置的爵士,也有99%的机会,该运行系统窗口将不会看起来像一个设计。这是它在我的 Windows 8.1 上的外观:
The initial solution is, like in Oren's answer to use the SizeTocontent
property. It basically tells WPF to define the window size from it's content (aka "client size"), instead of the window itself (aka "client size + all that non-client/chrome/border totally incontrollable stuff").
最初的解决方案是,就像在 Oren 使用该SizeTocontent
属性的答案中一样。它基本上告诉 WPF 从它的内容(又名“客户端大小”)而不是窗口本身(又名“客户端大小+所有非客户端/chrome/边框完全无法控制的东西”)定义窗口大小。
So, we can define the Content to be of fixed size, like this:
因此,我们可以将内容定义为固定大小,如下所示:
<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>
And now, the runtime window looks exactly how we want (Note the Grid's height and width don't have exactly the same values as the original Window one - 40/180 vs 75/190 -, and that's fine, as in design mode, you can now just forget window Height and Width property forever):
现在,运行时窗口看起来正是我们想要的(注意网格的高度和宽度与原始窗口一的值不完全相同 - 40/180 vs 75/190 - 这很好,就像在设计模式下一样,您现在可以永远忘记窗口的高度和宽度属性):
How does that behave when the window is resized? like this, the grid is centered, which is fine if you want that behavior:
调整窗口大小时它的行为如何?像这样,网格居中,如果你想要这种行为,这很好:
But, if we want the behavior in the question, ("I can resize the window, but its content doesn't resize with it, because I specified a fixed size."), which was the original behavior too, instead of specifying Width and Height, we can use MinWidth
and MinHeight
, like this:
但是,如果我们想要问题中的行为(“我可以调整窗口大小,但它的内容不会随之调整大小,因为我指定了固定大小。”),这也是原始行为,而不是指定 Width和高度,我们可以使用MinWidth
和MinHeight
,像这样:
<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>
The runtime window looks the same, but the resize experience is now comparable to the original default Window layout:
运行时窗口看起来相同,但调整大小的体验现在可与原始默认窗口布局相媲美:
That should be the default WPF designer layout IMHO.
恕我直言,这应该是默认的 WPF 设计器布局。
回答by Sharun
I do the following in the constructor and add ResizeMode="CanResizeWithGrip" in the xaml, but it kind of depends on how much space your content occupies on startup
我在构造函数中执行以下操作并在 xaml 中添加 ResizeMode="CanResizeWithGrip",但这取决于您的内容在启动时占用多少空间
public Window1()
{
this.Height = SystemParameters.WorkArea.Height;
this.Width = SystemParameters.WorkArea.Width;
}
回答by ergohack
Simon Mourierposts an excellent answer, but I needed the size of one control to defer to the others. So inverting the window sizing behavior with the SizeToContent
attribute was not what I needed. I ended up following [Tim's][method to compute the non-client size]to subtract out the non-client area from the MainWindow's dynamic Width and Height; in a XAML <MultiBinding>
element. This was accomplished through reading the SystemParameters.WindowCaptionHeight
and SystemParameters.ResizeFramVerticalBorderWidth
properties (see the IMultiValueConverter
code below).
Simon Mourier发布了一个很好的答案,但我需要一个控件的大小来服从其他控件。因此,使用SizeToContent
属性反转窗口大小调整行为并不是我所需要的。我最终遵循[Tim 的] [计算非客户端大小的方法]从 MainWindow 的动态宽度和高度中减去非客户端区域;在 XAML<MultiBinding>
元素中。这是通过读取SystemParameters.WindowCaptionHeight
和SystemParameters.ResizeFramVerticalBorderWidth
属性来完成的(参见IMultiValueConverter
下面的代码)。
<Grid>
<Grid.RowDefinitions>
<RowDefinition x:Name="_mwR0" Height="Auto"/>
<RowDefinition x:Name="_mwR1" Height="4*"/>
<RowDefinition x:Name="_mwR2" Height="Auto"/>
<RowDefinition x:Name="_mwR3">
<RowDefinition.Height>
<MultiBinding Converter="{StaticResource SizeToRemainderConverter}" Mode="TwoWay">
<Binding ElementName="_LogWindow" Path="Height" Mode="TwoWay"/>
<Binding ElementName="_MainWindow" Path="Height" Mode="OneWay"/>
<Binding ElementName="_mwR0" Path="Height" Mode="OneWay"/>
<Binding ElementName="_mwR1" Path="Height" Mode="OneWay"/>
<Binding ElementName="_mwR2" Path="Height" Mode="OneWay"/>
</MultiBinding>
</RowDefinition.Height>
</RowDefinition>
</Grid.RowDefinitions>
<Menu IsMainMenu="True" Grid.Row="0">...</Menu>
<ListView Grid.Row="1">...</ListView>
<GridSplitter Grid.Row="2" ShowsPreview="True" HorizontalAlignment="Stretch" Height="6" VerticalAlignment="Center" Margin="0"/>
<RichTextBox x:Name="_LogWindow" Grid.Row="3"/>
</Grid>
_LogWindow
is the inner control to the last row of the grid. As the MainWindow is resized, I need to shrink this control in deference to the others.
_LogWindow
是网格最后一行的内部控件。随着 MainWindow 调整大小,我需要根据其他控件缩小此控件。
The converter is complicated by having to handle both System.Double
and System.Windows.GridLength
object types. I also preserve the layout between application sessions, so I needed the converter to be bi-directional (apologies for the dense code).
转换器由于必须同时处理System.Double
和System.Windows.GridLength
对象类型而变得复杂。我还保留了应用程序会话之间的布局,因此我需要转换器是双向的(为密集代码道歉)。
public class SizeToRemainderConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parm, System.Globalization.CultureInfo culture)
{
double ret = 0.0;
if (values != null && values.Length > 0)
{
if (values[0].GetType().Name.Equals("String")) double.TryParse((values[0] as string), out ret);
else if (values[0].GetType().Name.Equals("GridLength")) ret = ((GridLength)values[0]).Value;
else ret = (double)System.Convert.ChangeType(values[0], typeof(double));
}
double available = 0.0;
if (values != null && values.Length > 1)
{
if (values[1].GetType().Name.Equals("String")) double.TryParse((values[1] as string), out available);
else if (values[1].GetType().Name.Equals("GridLength")) available = ((GridLength)values[1]).Value;
else available = (double)System.Convert.ChangeType(values[1], typeof(double));
available -= SystemParameters.WindowCaptionHeight;
available -= SystemParameters.ResizeFrameVerticalBorderWidth;
available -= SystemParameters.ResizeFrameVerticalBorderWidth;
}
for (int i = 2; i < (values?.Length ?? 0); ++i)
{
double delta = 0.0;
if (values[i].GetType().Name.Equals("String")) double.TryParse((values[i] as string), out delta);
else if (values[i].GetType().Name.Equals("GridLength")) delta = ((GridLength)values[i]).Value;
else delta = (double)System.Convert.ChangeType(values[i], typeof(double));
available -= delta;
}
if (available < ret) ret = 0.0;
if (targetType.Name.Equals("GridLength")) return new GridLength(ret);
return System.Convert.ChangeType(ret, targetType);
}
public object[] ConvertBack(object v, Type[] t, object p, System.Globalization.CultureInfo c)
{
object[] ret = new object[t.Length];
switch (v.GetType().Name)
{
case "Double":
for (int i = 0; i < t.Length; ++i)
{
if (t[i].Name.Equals("String")) ret[i] = v.ToString();
else if (t[i].Name.Equals("GridLength")) ret[i] = new GridLength((v as double?) ?? 0.0);
else ret[i] = System.Convert.ChangeType(v, t[i]);
}
break;
case "GridLength":
GridLength gl = (v as GridLength?) ?? new GridLength();
for (int i = 0; i < t.Length; ++i)
{
if (t[i].Name.Equals("String")) ret[i] = gl.Value.ToString();
else if (t[i].Name.Equals("GridLength")) ret[i] = gl;
else ret[i] = System.Convert.ChangeType(gl.Value, t[i]);
}
break;
case "String":
default:
double d = 0.0;
double.TryParse(v as string, out d);
for (int i = 0; i < t.Length; ++i)
{
if (t[i].Name.Equals("String")) ret[i] = v.ToString();
else if (t[i].Name.Equals("GridLength")) ret[i] = new GridLength(d);
else ret[i] = System.Convert.ChangeType(v, t[i]);
}
break;
}
return ret;
}
}
回答by zezba9000
Here is a fully native approch that doesn't require any XAML and works before the Window is shown.
这是一个完全原生的方法,不需要任何 XAML 并且在显示窗口之前工作。
public unsafe override void SetSize(int width, int height, WindowSizeType type)
{
if (type == WindowSizeType.WorkingArea)// aka client area
{
// get native HWND handle
IntPtr handle = new WindowInteropHelper(window).EnsureHandle();
// get window rect and size
RECT rect = new RECT();
int result = GetWindowRect(handle, ref rect);
if (result == 0) throw new Exception("GetWindowRect failed");
int rectWidth = rect.right - rect.left;
int rectHeight = rect.bottom - rect.top;
// get client rect and size
RECT clientRect = new RECT();
result = GetClientRect(handle, ref clientRect);
if (result == 0) throw new Exception("GetClientRect failed");
int clientRectWidth = clientRect.right - clientRect.left;
int clientRectHeight = clientRect.bottom - clientRect.top;
// increase size based on client side decoration delta
width = width + (rectWidth - clientRectWidth);
height = height + (rectHeight - clientRectHeight);
// apply new adjusted window size
result = SetWindowPos(handle, IntPtr.Zero, 0, 0, width, height, SWP_NOMOVE);
if (result == 0) throw new Exception("SetWindowPos failed");
}
else
{
window.Width = width;
window.Height = height;
}
}
#region SetSize native Helpers
[StructLayout(LayoutKind.Sequential)]
struct RECT
{
public int left, top, right, bottom;
}
private const string lib = "User32.dll";
[DllImport(lib, EntryPoint = "GetWindowRect")]
private extern static int GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[DllImport(lib, EntryPoint = "GetClientRect")]
private extern static int GetClientRect(IntPtr hWnd, ref RECT lpRect);
[DllImport(lib, EntryPoint = "SetWindowPos")]
private extern static int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private const int SWP_NOMOVE = 0x0002;
#endregion
回答by t-kldw
I don't know, why you would need complicated code behind for that. This always worked fine for me:
我不知道,为什么你需要复杂的代码。这对我来说总是很好:
<Window x:Class="Loadsheet_2._0.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:Loadsheet_2._0"
mc:Ignorable="d"
Title="MainWindow" MinHeight="635" MinWidth="1200" SizeToContent="WidthAndHeight" ResizeMode="CanResize">