James McDuffie
James McDuffie

Reputation: 78

How would I show or hide a Grid column based on boolean flag?

So I have two columns where I want to set the width of the column to 0 or * depending on a Boolean flag? I was thinking I could do it with a Converter, but I was hoping for a cleaner solution. Any thoughts?

Upvotes: 1

Views: 1212

Answers (1)

James McDuffie
James McDuffie

Reputation: 78

So I finally determined that the best solution was to use a Style with a DataTrigger on the actual column definition. This means that the column owns the responsibility to resize himself based on a Boolean property.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition>
            <ColumnDefinition.Style>
                <Style TargetType="ColumnDefinition">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ShowAdvanced}" Value="False">
                            <Setter Property="Width" Value="0" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ShowAdvanced}" Value="True">
                            <Setter Property="Width" Value="*" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ColumnDefinition.Style>
        </ColumnDefinition>
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
</Grid>

And Viola, I get to show or hide a column based on a single bool check!

Upvotes: 2

Related Questions