It is not possible to change the width of a parent element based on the width of a child. In your case, the column is the parent of the WrapPanel
, so the ActualWidth
of the WrapPanel
is not available until after the grid/column has already been sized.
Even if you wrote code to try and circumvent this, you would still run into an issue, as the measure/layout sequence would become re-entrant. Whenever the size of the column changed, it would re-layout all the child controls, one of which is the WrapPanel
. This would cause the ActualWidth
of the WrapPanel
to be changed -- effectively causing an infinite loop. To prevent this stack overflow scenario, the framework immediately detects any re-entrancy in the layout/measure cycle and throws an exception.
I'm not exactly sure what you are trying to achieve. Why do would you want the Labels in a WrapPanel
? Surely you would always want the 4 labels to be on the same line, or at least on two lines.
If this is the case, I would set the width of the second Column to "Auto" and put the labels in another container, i.e. one of the following:
If you want all the labels to wrap, as in a
WrapPanel
being forced to its minimum width, use:<StackPanel Grid.Column="1" Orientation="Vertical"> ... your labels here ... </StackPanel>
If you want all the labels to stay on the same line, use:
<StackPanel Grid.Column="1" Orientation="Horizontal"> ... your labels here ... </StackPanel>
If you want two lines use:
<StackPanel Grid.Column="1" Orientation="Vertical"> <StackPanel Orientation="Horizontal"> ... 1st 2 labels here ... </StackPanel> <StackPanel Orientation="Horizontal"> ... 2nd 2 labels here ... </StackPanel> </StackPanel>