Window Size when SizeToContent is not specified

后端 未结 4 1515
借酒劲吻你
借酒劲吻你 2021-01-22 21:23

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



        
相关标签:
4条回答
  • 2021-01-22 21:55

    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.

    0 讨论(0)
  • 2021-01-22 21:56

    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..
    0 讨论(0)
  • 2021-01-22 22:00

    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.

    0 讨论(0)
  • 2021-01-22 22:01

    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.

    0 讨论(0)
提交回复
热议问题