tags:

views:

231

answers:

4

When the following XAML used, the window size is not 5000x5000, but some small window where the button is cropped.

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" >
    <Button Width="5000" Height="5000">XXX</Button>
</Window>

From what I can tell, size I did not specify the SizeToContent attribute, the default is "Manual", so it will use *size of a window is determined by other properties, including Width, Height, MaxWidth, MaxHeight, MinWidth, and MinHeight. * From the WPF Windows Overview, it seems those other properties are FrameworkElement::MinHeight/Width, and FrameworkElement::MaxHeight. But since the default for the Mins are 0, the Maxs are Infinity and the Width/Height is Nan....what's going on? Where is WPF getting the window size?

Any pointers to the right direction would be appreciated.

A: 

I guess the OS has provided the window with these values. It might be related to how Windows OS remembers the size of any window before it was last closed.*

The same way I suppose windows provided a default height and width to your window when none of the dimensions where specified.

  • You could try it with notepad. Open it. Resize it. Close it. Now again open it. Hmm..
Trainee4Life
not really an answer. This IS how the "Manual" setting is defined (the size of a window when is is manually resized). The question is WHERE is it being obtained from.
moogs
Notepad is writing and reading its position to registry: [HKEY_CURRENT_USER\Software\Microsoft\Notepad] This is best practise for most applications including 3rd party.
bitbonk
A: 

I think the information you're looking for is buried in the documentation for FrameworkElement.Width (link).

In addition to acceptable Double values, this property can also be Double.NaN. This is how you specify auto sizing behavior. In XAML you set the value to the string "Auto" (case insensitive) to enable the auto sizing behavior. Auto sizing behavior implies that the element will fill the width available to it. Note however that specific controls frequently supply default values in their default styles that will disable the auto sizing behavior unless it is specifically re-enabled. [my emphasis]

So, from this, I would expect that the width/height of the Window when there are no other constraints set (such as MaxWidth, MinWidth, Width, etc.) is determined by the default style of a Window.

Edit:

Looks like it's more likely it comes from a function called by the Window class itself that takes into account all the constraints plus the monitor size.

DanM
+1  A: 

I can't find anything in MSDN that explicitly states what the size defaults are, but the Top, Left and WindowStartupLocation property descriptions say that they default to the normal Win32 defaults if not specified. I think it's reasonable to assume that the Height and Width properties would do the same.

If you don't want this to happen for your window, then you must explicitly set either the Width and Height or the SizeToContent property of the Window element depending on your requirements. The explicit size you set on the Button element will only have an effect on the Window if Window.SizeToContent = True.

Christian Hayter
Should say "if Window.SizeToContent = WidthAndHeight." SizeToContent takes an enumeration value, as opposed to boolean. (I assumed it was boolean at first too.)
skypecakes
It depends on the content. I would set `SizeToContent = WidthAndHeight` if the window was some sort of dialog box, but `SizeToContent = Manual` for a normal application UI window.
Christian Hayter
+1  A: 

If the width and height are not specified WPF uses 60% of the width of you primary screen (where the windows is actually displayed) and 60% of its height. This is done by the Application singleton where all Windows ultimately register themselfes.

bitbonk