【问题标题】:How to change size of WPF Window dynamically?如何动态更改 WPF 窗口的大小?
【发布时间】:2012-01-25 13:06:11
【问题描述】:

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

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

任何线索或样本?

【问题讨论】:

  • 窗口最大化时会发生什么?
  • 其实并没有最大化。只需要更改高度,因为新面板出现在底部。

标签: c# wpf xaml


【解决方案1】:

您可以通过编程方式更改窗口的大小和位置,只需为大小设置适当的 Width 和 Height 值,为位置设置 Top 和 Left。但它更容易。

关注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,您可以在大小更改后再次将窗口居中。

【讨论】:

  • 谢谢兄弟!我需要的最终设置是:Width="721" Height="Auto" SizeToContent="Height"
【解决方案2】:

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

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

Application.Current.MainWindow.Height = 420;

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

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2013-11-19
    • 2011-08-30
    • 1970-01-01
    • 2013-11-02
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    相关资源
    最近更新 更多