Introduction
This article details how you can restrict the windows size of your WPF application while at the same time retaining the option to be able to maximize it.
Quite often, Windows applications are required to operate in two window sizes - a normal window size and a maximized window size.
The ResizeMode property of the WPF Windows class allows you to set whether you can resize the window or not. However, there is no option to restrict the sizes that the window can be in.
In this article, I will demonstrate how you can restrict resizing of your windows application to a normal window and a maximized window size, while also allowing your controls to adjust their position accordingly.
Simple Notepad
For the purpose of this article, I will create a very simple notepad application. There is a toolbar on top, which has a couple of left aligned and one right aligned button. The main body of the window is covered with a tabs block with a multi-line textbox within it. At the bottom of the window is a status bar.
Since, the application needs to be maximizable, the ResizeMode of the window is set to "CanResize". We also set the MinWidth and MinHeight properties of the window to the same value as the width and height properties respectively. The window state is set ot "Normal"
With the resize mode of the window set to "CanResize", the window has a resizable border and can be resized. To address this, add a handler of the SizeChanged event and add the following code in the event handler
namespace WPFResizeExample
{
using System.Windows;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ApplicationWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (this.WindowState != WindowState.Maximized)
{
this.Width = this.MinWidth;
this.Height = this.MinHeight;
}
}
}
}
The window XAML file is shown below
<Window x:Class="WPFResizeExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF Notepad"
ResizeMode="CanResize"
Background="LightGray"
FontSize="16"
FontFamily="Arial"
MinHeight="400" MinWidth="600"
Height="400" Width="600"
WindowState="Normal"
WindowStartupLocation="CenterScreen"
x:Name="ApplicationWindow"
SizeChanged="ApplicationWindow_SizeChanged">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Width="{Binding ActualWidth, ElementName=ApplicationWindow}" Background="Gray" Foreground="White" FontSize="32" Grid.Row="0" Grid.Column="0" x:Name="Heading">
</Label>
<TabControl Grid.Row="1" Grid.Column="0" Width="{Binding ActualWidth, ElementName=ApplicationWindow}" Height="{Binding ActualHeight, ElementName=ApplicationWindow}">
<TabItem x:Name="FirstTab" Header="Document 1">
<StackPanel>
<TextBox Width="{Binding ActualWidth, ElementName=ApplicationWindow}" Height="{Binding ActualHeight, ElementName=ApplicationWindow}" AcceptsReturn="True">
</TextBox>
</StackPanel>
</TabItem>
</TabControl>
<DockPanel>
<Button VerticalAlignment="Bottom" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="0" Margin="0,0,2,2" Width="50" Height="50">
<Image Source="resource\Button-Add-icon.png"/>
</Button>
<Button VerticalAlignment="Bottom" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="0" Margin="0,0,2,2" Width="50" Height="50">
<Image Source="resource\Button-Close-icon.png"/>
</Button>
<Button VerticalAlignment="Bottom" HorizontalAlignment="Right" Grid.Row="0" Grid.Column="0" Margin="0,0,2,2" Width="50" Height="50">
<Image Source="resource\Button-Help-icon.png"/>
</Button>
</DockPanel>
<StatusBar Height="24" VerticalAlignment="Bottom" Grid.Row="1" Grid.Column="0" Background="Black" Foreground="White">
<TextBlock>Document not saved...</TextBlock>
</StatusBar>
</Grid>
</Window>