Forlani Forlani - 3 months ago 8
C# Question

How to set a resize limit for a window?

Lets say I have a xaml for my window like this:

<Window x:Class="WavePoint_MVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FontSize="13"
FontFamily="Verdana"
xmlns:local="clr-namespace:WavePoint_MVVM"
Height="{Binding SystemParameters.PrimaryScreenHeigth}"
Width="{Binding SystemParameters.PrimaryScreenWidth}"
AllowsTransparency="False"
WindowStartupLocation="CenterScreen"
ResizeMode="CanResizeWithGrip"
<Grid/>
</Window>


Now I want to fix some sort of limit in which the user can't resize the window beyond it. Just to give an example, in Spotify app for Windows desktop, the user can't resize beyond a certain limit. This is the minimum I can get:

enter image description here

Thoughts?

Answer

To force a minimum window size, set your MinWidth and MinHeight attributes on the Window:

<Window x:Class="WavePoint_MVVM.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    FontSize="13" 
    FontFamily="Verdana"
    xmlns:local="clr-namespace:WavePoint_MVVM"
    Height="{Binding SystemParameters.PrimaryScreenHeigth}" 
    Width="{Binding SystemParameters.PrimaryScreenWidth}"
    AllowsTransparency="False"
    WindowStartupLocation="CenterScreen"
    ResizeMode="CanResizeWithGrip"
    MinWidth="640"
    MinHeight="480">
   <Grid/>
</Window>