WPF Window - Only allow horizontal resize
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;}
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.