Click here to Skip to main content
13,096,268 members (56,893 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 19 Feb 2014

Restricting WPF applications to be operate in two window sizes

, 19 Feb 2014
Rate this:
Please Sign up or sign in to vote.
Demonstrates how to restrict your WPF application to a specific window size while keeping it maximizable.


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;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()

        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"
        Title="WPF Notepad" 
        MinHeight="400" MinWidth="600" 
        Height="400" Width="600"
      <RowDefinition Height="55"/>
      <RowDefinition Height="*"/>
      <ColumnDefinition Width="*"/>

    <Label Width="{Binding ActualWidth, ElementName=ApplicationWindow}" Background="Gray" Foreground="White" FontSize="32" Grid.Row="0" Grid.Column="0" x:Name="Heading">
    <TabControl Grid.Row="1" Grid.Column="0" Width="{Binding ActualWidth, ElementName=ApplicationWindow}" Height="{Binding ActualHeight, ElementName=ApplicationWindow}">
      <TabItem x:Name="FirstTab" Header="Document 1">
          <TextBox Width="{Binding ActualWidth, ElementName=ApplicationWindow}" Height="{Binding ActualHeight, ElementName=ApplicationWindow}" AcceptsReturn="True">              
      <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 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 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"/>
    <StatusBar Height="24" VerticalAlignment="Bottom" Grid.Row="1" Grid.Column="0" Background="Black" Foreground="White">
      <TextBlock>Document not saved...</TextBlock>


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hamid Shahid Aijazi
United Kingdom United Kingdom
Hamid is a Microsoft Visual Studio ALM Ranger, a life long developer, a dad and a part time husband. In his day job he is an independent consultant with expertise in Microsoft .Net technologies.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 19 Feb 2014
Article Copyright 2014 by Hamid Shahid Aijazi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid