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

时间:2012-01-25 13:06:12

标签: c# wpf xaml

假设我们展示了一些WPF窗口,我们必须在底部显示一些额外的面板。

我想要做的是增加WPF窗口大小并再次居中。

任何线索或样品?

2 个答案:

答案 0 :(得分:47)

您可以通过编程方式更改窗口的大小和位置,只需为大小设置相应的宽度和高度值,为位置设置“上”和“左”。但它更容易。

关注this page

<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"> 

自动调整窗口大小以适应内容,使用help of this link,您可以在更改大小后再次将窗口置于中心位置。

答案 1 :(得分:4)

如果您想以特定尺寸调整大小,可以执行以下操作:

如果要调整主窗口的大小,只需编写以下代码。

Application.Current.MainWindow.Height = 420;

如果要调整主窗口以外的新窗口,只需在新窗口的.cs文件中编写以下代码。

Application.Current.MainWindow = this; 
Application.Current.MainWindow.Width = 420;

希望它有所帮助。

相关问题