0

I have a Grid which has two columns, first column has a DataGrid and second column has another grid. I expect DataGrid to expand to take full space when Visibility of second grid is set to 'collapsed'. Following is the code snippet:

<Grid Grid.Row="1" HorizontalAlignment="Left" Width="344">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="24"/>
    </Grid.ColumnDefinitions>
    <DataGrid MaxWidth="344" Grid.Column="0" SelectedItem="{Binding Dummy, Mode=TwoWay}" Background="DarkGray"
              ItemsSource="{Binding DummyList}" SelectionMode="Single" AutoGenerateColumns="False" RowHeaderWidth="0" GridLinesVisibility="All">

        <DataGrid.Columns>
            <DataGridTemplateColumn Header="" Width="35">
                <DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="" Width="*" MinWidth="85">
                <DataGridTemplateColumn.CellTemplate>

                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="" Width="*" MinWidth="90" >
                <DataGridTemplateColumn.CellTemplate>

                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="" Width="*" MinWidth="80">
                <DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="" Width="*" MaxWidth="25" Visibility="Collapsed">
                <DataGridTemplateColumn.CellTemplate>

                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

    <Grid Grid.Column="1" Margin="0,1,0,0" Background="DarkGray" Visibility="Collpased">
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
    </Grid>
</Grid>

Even when I set visibility of Grid in second column to collapsed, DataGrid do not takes up the whole space, second column remains empty. Do I have wrong expectation, If yes/no, how can I achieve this behavior?

1 Answer 1

0

second column has a fixed width <ColumnDefinition Width="24"/>, even if it doesn't contain child elements (ot if they are collapsed).

change width to Auto <ColumnDefinition Width="Auto"/> and give necessary width to child Grid: <Grid Grid.Column="1" Width="24". Then column will react to content visibility

1
  • It worked, I was missing the concept of Auto..thanks a ton!
    – A. Trivedi
    Commented Mar 17, 2020 at 10:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.