239

What I am trying to do is show a window, that does not explicitly have a height/width, (both values omitted or set to Auto). I was guessing that the window would find out its size by auto - calculating all contained usercontrols sizes, but this doesn't actually work!

Instead I get a big window with Actualwidth and Actualheight values both set to 512 (?!?!)

Window declaration:

<Window x:Class="Window3"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window3" 
  Height="Auto">
<StackPanel>
    <Label>Window</Label>
</StackPanel>
</Window>

Showing this window as a dialog via:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
    Dim dlg As New Window3
    dlg.ShowDialog()
End Sub

Is there a solution for this? I don't want to explicitly set the size of my window because many controls in the form will be collapsed based on constructor parameters, and trying to find the actual size of the form would be tricky (and ugly).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nikos Tsokos
  • 3,226
  • 2
  • 34
  • 41

5 Answers5

454

Set the window's property SizeToContent="WidthAndHeight". This should help.

Welcor
  • 2,431
  • 21
  • 32
Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
  • 92
    For lazy people that want to copy paste `SizeToContent="WidthAndHeight"` ;) – Tono Nam Jan 12 '15 at 21:56
  • 6
    Careful to use SizeToContent="WidthAndHeight", If the size of content of window increases more than the screen size, window can overflow from screen. – Kylo Ren Apr 23 '16 at 18:00
  • 2
    @KyloRen so a MaxWidth or MaxHeight should be set to avoid the problem – Carlos Liu May 23 '19 at 01:22
  • @CarlosLiu long time, I'm out of practice and can't remember, but yes logically sounds right. try in a demo :) – Kylo Ren May 23 '19 at 04:11
  • This is great, but I'm still curious why `Height="Auto"` and `Width="Auto"` doesn't accomplish the same thing? I'm guessing because window content isn't considered with these settings. – Mike Lowery Feb 03 '20 at 20:57
9

Old question but an updated answer:

As @Muad'Dib suggested in his answer :

you should set SizeToContent="WidthAndHeight".

If the size of content of window increases more than the screen size, window can overflow from screen. So, you must consider:

MaxWidth="600"
MaxHeight="400"
Vishal
  • 6,238
  • 10
  • 82
  • 158
3

Well you can't set the window height to auto, to do this you can use a little trick, name the main main grid container, set its height to auto then bind the window height to the height of the main grid

  • This approach requires care, since you don't want the height of the whole window to match the height of the main grid; you want the height of the content area of the window to match the height of the grid. – M Kloster Jan 22 '21 at 14:50
1

You can do the following:

MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
SizeToContent = SizeToContent.WidthAndHeight;

Please note that using SizeToContent alone will work. However, if your content size is bigger than the screen the window will overflow beyond your screen but combining this with setting the MaxWidth and MaxHeight of the window to be as your screen will basically fit the window to contents up to the maximum of your screen.

Zeyad
  • 537
  • 2
  • 7
  • 15
-1

Set size in curent screen with loaded event windows

Hooman
  • 49
  • 1
  • 4