0
votes

I have a StackPanel that is vertically stacking a DockPanel on top of a DataGrid:

<StackPanel Orientation="Vertical"...etc...
    <DockPanel>
        <Label DockPanel.Dock="Left"...etc... />
        <Button DockPanel.Dock="Right" ...etc.../>
    </DockPanel
    <DataGrid ...etc...
    </DataGrid
</StackPanel

The DataGrid is bound to a collection so its number of columns is only known at runtime. I'd like the Label control to align with the left edge of the DataGrid and the Button control to align with the right edge, hence the arrangement of panels I'm using. The trouble is that the StackPanel does not constrain its width to that of its content but rather it expands to the width of its container. The result is that the Button will extend beyond the right edge of the DataGrid control as the StackPanel's container (the window) gets progressively larger. How do I constrain the StackPanel dimensions to the total dimensions of its content?

1

1 Answers

1
votes

The trick is place the StackPanel in the left-most column of a two-column Grid:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <StackPanel Grid.Column="0" ... etc...>
    </StackPanel>
</Grid>