C# WPF Window Width, MaxWidth, MinWidth ignored

24,639

Solution 1

You have set MinWidth to 70 and therefore size of your Window cant be less than that. BTW because of control box it's width seems to have a minimum limit of 132.

If we set WindowStyle="none" to remove the title and control box, we can make the Window even smaller.

<Window x:Class="WpfApplicationUnleashed.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApplicationUnleashed"
        Title="" WindowStyle="None" Width="70">
    <Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    </Grid>
</Window>

EDIT

To make the Window width to 70, while the close button, Title text still visible and no-resize use this:

<Window x:Class="WpfApplicationUnleashed.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApplicationUnleashed"
        Title="My Window" WindowStyle="ToolWindow" Width="70" ResizeMode="NoResize">
    <Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    </Grid>
</Window>

Since 70 is a very small width, you cant have minimize and maximize button along with close button.

Solution 2

70 that you have specified is not in pixels. WPF works on points and it actually sizes window to pixels based on Screen DPI (Dots per Inch).

Share:
24,639
Richard
Author by

Richard

Updated on March 17, 2020

Comments

  • Richard
    Richard about 4 years

    I have a WPF Window defined in XAML like this:

    <Window x:Class="com.some.company.window"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="My Cool Window" 
    x:Name="CoolWindow"
    Height="435"
    Width="70"
    MinWidth="70"
    MaxWidth="70"
    Left="{PropertyState Default=0}"
    Top="{PropertyState Default=0}"
    Initialized="InitializeWindow"
    ResizeMode="NoResize"  
    Style="{DynamicResource DefaultWindow}">
    .....
    .....
    </Window>
    

    The problem is that when the Window is created and displayed on the screen - it is ALWAYS larger than the 70 pixels I specified in the width definition. The width is probably 80-90 pixels. My width attributes are ignored. None of the contents inside the Window are larger than 70 pixels either.

    Even when I try to resize the window with the grips, it will not let me resize it below a specific width. Is there some reason WPF is not letting me set the width of the window smaller? Is there a hidden minimum width value for every window? and how would I get around this?

    EDIT: When I add WindowsStyle="None" into the Window attribute, the width is correctly set to 70 pixels. However, this is not the style I want for the Window.

    Thanks