Is it possible to use Triggers on Grid RowDefinitions?
Asked Answered
B

2

18

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?

Bruno answered 24/2, 2011 at 13:41 Comment(0)
O
26

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>
Outofdoor answered 24/2, 2011 at 14:10 Comment(2)
Yep. As soon as I removed the explicit setting of Height, the problem went away. Good catch!Bruno
Why should I indicate RowDefinition.Height and why doesn't work with only Height as Property? I don't understand itHaematogenesis
U
9

I know this post is 3 years old, but still it might help someone.

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

I was in same situation, i managed to get it working by setting TargetType to RowDefinition.

Unpriced answered 8/8, 2014 at 6:22 Comment(1)
RowDefinition.Height can be reduced to HeightPampas

© 2022 - 2024 — McMap. All rights reserved.