Question

I'm trying to implement a certain layout.

I have two elements that I want to stack vertically (I need them to follow each other closely). I am currently trying to achieve it using a Stackpanel.

The problem is that I want the first element to have a limited width and the other to use all the width available in the StackPanel. Ideally, I would like that the first element have a width equals to the width of four columns from the grid that contains the StackPanel, here is my code.

<Grid>
<!-- Colums and Rows definition go here -->
<StackPanel  Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="3" Grid.RowSpan="8">
    //The first element
    <Viewbox Name="viewbox_choix" Margin="160,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="3" Grid.RowSpan="4">
    //The second element
    <StackPanel Grid.Column="0" Grid.ColumnSpan="5">
        <Border></Border>
        etc...
    </StackPanel>
</StackPanel>
</Grid>

The grid attributes are referring to the parent grid of the stackpanel. But the Grid.Column and Grid.ColumnSpans seem to have no effect when I try to use them inside the StackPanel.

The problem of that code is that the first element also uses all the width of the StackPanel but that isn't what I want...

Can anybody help me ? I precise that I'm still learning WPF and I don't really know how bindings work...

Was it helpful?

Solution

In WPF, a StackPanel does not work like a Grid. There is no maximum width... it will happily let content disappear out of its right side. If you want automatic resizing, just replace the StackPanels with `Grid

UPDATE >>>

In the Grid class, there is an attached property called IsSharedSizeScope. Add this to the parent Grid and set it to true. Then in your RowDefinitions, you can add SharedSizeGroup properties to the columns that you require.

These examples may help you:

Grid's SharedSizeGroup and * sizing (SO post)

Grid.IsSharedSizeScope Attached Property (MSDN)

You may need to experiment a bit, but you should be able to get the desired effect using these properties.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top