简体   繁体   English

如何动态更改WPF窗口的大小?

[英]How to change size of WPF Window dynamically?

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? 任何线索或样品?

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. 但它更容易。

Following this page you 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 link you can center the window again after the size was changed. 自动调整窗口大小到内容,并在此链接帮助下,您可以在更改大小后再次居中窗口。

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. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM