WPF窗口 - 仅允许水平调整大小

Dav*_*ard 17 wpf resize window

我想只允许我的WPF窗口水平调整大小.我怎样才能做到最好?

Use*_*621 31

如果您想使用MinHeightMaxHeight方法但仍允许窗口自动调整大小以适合其内容的大小:

<Window x:Class="MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        SizeToContent="WidthAndHeight"
        ResizeMode="CanResize"
        Loaded="window_Loaded">
Run Code Online (Sandbox Code Playgroud)

在代码隐藏中:

private void window_Loaded(object sender, RoutedEventArgs e)
{
    this.MinWidth = this.ActualWidth;
    this.MinHeight = this.ActualHeight;
    this.MaxHeight = this.ActualHeight;
}
Run Code Online (Sandbox Code Playgroud)

  • 如果窗口内容发生变化并且窗口大小必须动态变化,这将无济于事 (2认同)

小智 27

如果将窗口的MinHeightMaxHeight属性设置为所需的高度,则窗口将具有固定的高度

  • 遗憾的是,与基于内容大小的自动高度计算不兼容. (10认同)