To solve the same problem, I wrote a special container class:
class FrugalContainer : Decorator { protected override Size MeasureOverride(Size availableSize) { return new Size(0, 0); } protected override Size ArrangeOverride(Size arrangeSize) {
Surround your ListBox with a container, and the height of the ListBox will be the same as Expander.
<Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> </Grid.RowDefinitions> <FrugalContainer Grid.Row="0" Grid.Column="0" > <ListBox /> </FrugalContainer> <Expander Grid.Row="0" Grid.Column="1" Header="Expander" /> </Grid>
Note that I am removing Width="Auto" from the column definition because the FrugalContainer will be as small as possible. Therefore, you cannot set the width or height of the parent grid cell to Auto.
If you need automation, rewrite the container:
class FrugalHeightContainer : Decorator { protected override Size MeasureOverride(Size availableSize) { Child.Measure(availableSize); return new Size(Child.DesiredSize.Width, 0); } protected override Size ArrangeOverride(Size arrangeSize) { Child.Measure(arrangeSize); Child.Arrange(new Rect(arrangeSize)); return Child.RenderSize; } }
Lu55
source share