7

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

1
  • Maybe some of the window's contents are larger than your current window size? Commented Mar 16, 2011 at 14:59

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I want the width to be exactly 70, but when the Window displays, it's always around 80-90 pixels instead of the 70 pixels i want
remove the control box as i suggested and then you can set width to 70 by setting Width=70 on window.
I know if I use WindowStyle="none", it will respect the width=70. However, I cannot use WindowStyle="none" because my application requires the Windows chrome and close buttons in the title bar. What control box are you referring to?
try setting WindowStyle="ToolWindow" Width="70" ResizeMode="NoResize". This does not remove the close button.
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).

2 Comments

I guess by pixels he is referring to DIU(Device Independent Units) :)
If your desktop scaling (Windows setting, not an app setting) is set to 125%, then a WPF window size (Width, MinWidth, MaxWidth, ActualWidth) of 70 will be 70x1.25=87.5 physical pixels. There are ways to de-scale WPF apps back to hardware pixels, but it's not recommended because it causes issues in very-high DPI contexts.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.