Pavel Matuska
Pavel Matuska

Reputation: 777

Why does MeasureOverride receive the availableSize?

If a parent control asks its children "How big do you want to be?", then what use is the availableSize parameter that's passed along? I've taken a peek via Reflector into the StackPanel's source and I still can't figure it out.

If the child wants to be 150x30, then it still reports 150x30 even if availableSize is 100x20, doesn't it? And if the child is expected to constrain itself to the availableSize, then that might as well be done on the size that's returned from calling MeasureOverride on the child - no point in passing that parameter.

Is there something that I'm not taking into account?

Upvotes: 2

Views: 220

Answers (3)

Peter Huber
Peter Huber

Reputation: 3312

One has to differentiate between the following possibilities:

Container

  • Container has unlimited size (ScrollViewer) or wants to know how much space the child needs. In this case the availableSize in MeasureOverride() is infinite.
  • Container has limited size. In this case the availableSize in MeasureOverride() is the limited size.

Child

  • Child can adjust to Container size. In this case it returns availableSize if availableSize is not infinite. If it is infinite and Child cannot calculate size on its own, it can return 0.
  • Child has a fixed size. In this case it does not care about the availableSize but returns the fixed size.

The same thing happens again in ArrangeOverride(Size arrangeBounds), although with possibly different sizes. Therefore, do not use values calculated in MeasureOverride(), but recalculate them in ArrangeOverride().

arrangeBounds cannot be infinite. Instead of infinite, the container passes the available size calculated in its Arrange() method. The child can still use a different size. If it is too big, the container will clip it. If it is too small, the Container needs to align it somehow (ContentAlignment).

If a control (Container, Child) is fixed size or not depends also on properties like Width, MinWidth, MaxWidth, HorizontalAlignment, etc. Depending on these settings, a control demands in some parameter combinations a fixed size and in others it can adjust to the available size.

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102753

If the child wants to be 150x30, then it still reports 150x30 even if availableSize is 100x20, doesn't it?

It depends on the control, but generally the answer is no. In any case, the point is to give it the opportunity to fit itself to the container, but it is not required to do so.

Think about the difference between a Grid and a StackPanel. The Grid will typically size itself precisely to the available size. The StackPanel, by contrast, will size itself infinitely in one direction only (depending on its orientation), regardless of the available size. In the other direction, it will extend itself only as far as the space needed for its children, unless its "HorizontalAlignment" / "VerticalAlignment" is set to "Stretch", in which case it will stretch itself out to the available size in that direction.

The ViewBox is a more complex example that makes good use of "availableSize". It generally sizes itself to the available space, and scales/stretches its children depending on the values of "Stretch" and "StretchDirection".

Upvotes: 3

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

The point is to give the element the opportunity to size itself correctly. After all the parent control might clip it if it doesn't respect the available size.

Upvotes: 0

Related Questions