Is it possible to use Triggers on Grid RowDefinitions?

GWLlosa picture GWLlosa · Feb 24, 2011 · Viewed 7.3k times · Source

I have a grid whose rows need to be resized dynamically based on the view model. I'd like to do something like the following:

<RowDefinition Height="2*">
    <RowDefinition.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=ShowSection}" Value="True">
                    <Setter Property="RowDefinition.Height" Value="2*"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=ShowSection}" Value="False">
                    <Setter Property="RowDefinition.Height" Value="0"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </RowDefinition.Style>
</RowDefinition>

This compiles, throws no errors, but doesn't seem to have any effect. Is there something I'm missing, or does the Grid not allow its rows to resize after the form is drawn or something to that effect?

Answer

Fredrik Hedblad picture Fredrik Hedblad · Feb 24, 2011

I think the only problem with your Xaml code is that you're overwriting the DataTrigger by setting Height explictly on the RowDefinition. Try with using a Setter instead

<RowDefinition>
    <RowDefinition.Style>
        <Style>
            <Setter Property="RowDefinition.Height" Value="2*"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=ShowSection}" Value="True">
                    <Setter Property="RowDefinition.Height" Value="2*"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=ShowSection}" Value="False">
                    <Setter Property="RowDefinition.Height" Value="0"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </RowDefinition.Style>
</RowDefinition>