Add/Subtract GridLength in WPF

2.5k views Asked by At

I have three columns containing two expanders in left and right columns. When both the expnders collapsed, I need to arrage the content in the middle column on the full window. For that I need to calculate the gridlengths.

For Example

GridLength w1= new GridLength( 20 );
GridLength w2= new GridLength( 50 );
GridLength w3= new GridLength( 0 );

How to get

w3 = w2 - w1
1

There are 1 answers

0
Pieter Breed On BEST ANSWER

Try this: Set the widths of the expandable ColumnDefinitions to Auto and the middle one to 1 star-unit. Control the width of the Expandable columns by setting the width on their contents. Then when they collapse, the middle column should expand to fill the available space.

Example:

<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Expander Margin="0" VerticalAlignment="Top" Header="Expander" ExpandDirection="Right">
        <Grid>
            <Grid HorizontalAlignment="Left" Width="100" Background="Blue" Height="100"/>
        </Grid>
    </Expander>     
    <Expander Grid.Column="2" Margin="0" VerticalAlignment="Top" Header="Expander" ExpandDirection="Left">
        <Grid>
            <Grid HorizontalAlignment="Left" Width="100" Background="Blue" Height="100"/>
        </Grid>
    </Expander>
    <Grid Background="Aquamarine" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1" />
</Grid>

Hope this helps