Why are my WPF window sizes defaulting to be huge

后端 未结 3 1135
清酒与你
清酒与你 2020-12-20 11:16

I have a wpf application with a few forms. At design time they are small, and they are not set to auto size. However at run time they are giant, even with no content in them

相关标签:
3条回答
  • 2020-12-20 11:48

    Why don't you just add a:

    Height="296" Width="634"
    

    to the Window definition? I'm not sure where the default size comes from but I've set specific sizes on my windows to get around this. Then I dynamically resize in the application startup code.

    I'm hoping that WPF will have a pack-like feature where it recursively sets controls based on the preferred sizes of the sub-controls (Swing and wxPython both had this feature) so you may want to go looking for that.


    Actually, a bit of searching has turned up the Window.SizeToContent property which may be what you're looking for. Its default is Manual but, if you set it to one of the other values (Width, Height or WidthAndHeight), you may see a better presentation.

    In fact, I just nicked over to the Windows laptop and entered:

    SizeToContent="WidthAndHeight"
    

    into the Window definition and it looks perfectly sized. So, I'd suggest giving that a try and seeing if it fixes (or at least works around) your problem.

    0 讨论(0)
  • 2020-12-20 11:51

    If you create a <Window/> with this code:

    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
    

    Then when you run that from Visual Studio it will display a window the same size as the one you put in your question. So this isn't anything you explicitly did.

    What you've encountered is the default computed size of a WPF Window. For different results, specify a SizeToContent parameter on the Window, as paxdiablo pointed out, or explicitly set the size of your window with the MinHeight, MaxHeight, Height, MinWidth, MaxWidth, and/or Width properties.

    0 讨论(0)
  • 2020-12-20 11:58

    In design you are seeing small because you have blend design properties for the window.

    d:DesignHeight="296" d:DesignWidth="634"

    Since Grid is a dynamic layout it will be stretch to the window's width & height. To restrict you window size you need to mention Height & Width properties to the window.

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