WPF Window - Only allow horizontal resize

You could try databinding to the size of the window, and then setting the size back to the old value whenever the vertical dimension is changed.


It's kind of a pain. Basically you need to set up a hook function to process windows messages. Then you would catch the WM_SIZING (0x0214) message and modify the parameters so that the horizontal dimension could not be changed.

Pete Brown also has some great info on this topic on his blog.


If you want to use the MinHeight and MaxHeight approach but still allow the window to automatically size itself to fit the size of its content:

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

In code-behind:

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

If you set the MinHeight and MaxHeight attributes of the window to the desired height the window will have a fixed height

Tags:

Wpf

Resize

Window