tags:

views:

100

answers:

2

I'm trying to figure out how come WPF doesn't go crazy when I set a window to resize to its content and then set its content to take all the available space... Any idea?

+1  A: 

It's not that difficult: if the window is set to stretch to match its content, then it doesn't have a static size, so it assumes default dimensions. Going down the visual tree, the control wants to stretch to fill the window -- it doesn't force any dimensions, but instead accepts whatever size the window has (the default one).

My guess is that elements always have a size (default one if none specified above them in the hierarchy) and the mention whether that size is fixed or negotiable.

I'm only guessing here, but it's just to prove that the situation is not as bad as it seems. With a good update strategy in place, the elements can adjust in relation to each other in a sane manner.

Tiberiu Ana
Ok. I've just made another test with a textbox. When I add text to it, the window stretch to accommodate the content. So the content is sort of forcing a dimension. What happen in that case?
Subb
What could be happening is that the TextBox is allowed to resize, and the window is set to allow that. In this case there is no more loop, because the TextBox has some dimensions it wants to obtain, namely the size of the content -- not connected to the size of the window anymore. The change in dimensions propagates up the visual tree, and the window is happy to oblige by resizing too. This does not cause the TextBox to resize again.
Tiberiu Ana
+4  A: 

There are two passes in the layout system - measure and arrange.

First in the measure pass each child is given a proposed available size, and this propagates down the tree. This sets each child's DesiredSize property.

In the next, arrange pass, the DesiredSize is taken into account, but the parent has the final word on how much actual available size it will give to a child, and the parent places each child accordingly, informing it of the actual size it gets, and so on down the tree.

Also, consider several imaginable corner cases:

  • The child demands double.Infinity desired size: explicitly not allowed, raises an exception
  • The child sets its HorizontalAlignment to "Stretch": the parent doesn't offer infinite actual size
  • Window set to SizeToConent, child set to "Stretch", no other constraint set:
    1. Window first offers infinite measure size
    2. Child demands the size measured by its children (text width, margins, etc) - not infinite size
    3. Arrange: window sets its size to a system-defined minimum or as desired by child, whichever is greater (can't be infinite)
    4. The child stretches to fit the size determined in previous step
kek444
Nicely explained.
Stimul8d
Yes, thank you. For others, I've been reading http://msdn.microsoft.com/en-us/library/ms745058.aspx and also using Refactor to look at the actual WPF code.
Subb
'Welcome. You mean *Reflector*, right? :)
kek444
oh, humm, yes :P
Subb