简体   繁体   中英

C# WPF Window Width, MaxWidth, MinWidth ignored

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

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.

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).

I was able to do what I wanted by adding WindowStyle="ToolWindow" and ResizeMode="NoResize". This allowed me to keep the titlebar and the close button, while allowing the window's width to be set at 70pt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM