C# 如何动态更改 WPF 窗口的大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9003212/
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 change size of WPF Window dynamically?
提问by Developer
Let's say we show some WPF Window and comes moment when we have to show some extra panel at the bottom.
假设我们显示了一些 WPF 窗口,然后我们必须在底部显示一些额外的面板。
What I want to do is to increase WPF Window size and center it again.
我想要做的是增加 WPF 窗口大小并再次居中。
Any clue or samples?
任何线索或样本?
采纳答案by Matten
You can programmatically change the size and location of the window, just set the appropriate Width and Height values for size and Top and Left for location. But it's even easier.
您可以以编程方式更改窗口的大小和位置,只需为大小设置适当的 Width 和 Height 值以及为位置设置 Top 和 Left 即可。但它更容易。
Following this pageyou get
按照这个页面你会得到
<Window x:Class="SizingTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Width="Auto" Height="Auto" SizeToContent="WidthAndHeight">
to automatically adapt the window size to the content, and with the help of this linkyou can center the window again after the size was changed.
回答by Jhossep Augusto Popayán Avila
if you want to resize at a specific size you can do the following:
如果要以特定大小调整大小,可以执行以下操作:
If you want to resize the main window just write the following code.
如果要调整主窗口的大小,只需编写以下代码。
Application.Current.MainWindow.Height = 420;
If you want to resize a new window other than the main window just write the following code in the .cs file of the new window.
如果要调整主窗口以外的新窗口的大小,只需在新窗口的 .cs 文件中编写以下代码即可。
Application.Current.MainWindow = this;
Application.Current.MainWindow.Width = 420;
Hope it helps.
希望能帮助到你。

