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?
The following example creates a Grid with three rows. The Height of the first row is set to the value Auto, which distributes height evenly based on the size of the content that is within that row. The height of the second row and third row are set to 2* and * respectively. The second row gets 2/3 of the remaining space and the third row gets 1/3.
Grid. Row Definitions Property Xamarin. Forms Provides the interface for the bound property that gets or sets the collection of RowDefinition objects that control the heights of each row. A RowDefinitionCollection for the Grid instance. RowDefinitions is an ordered set of RowDefinition objects that determine the height of each row.
Xaml. Controls Gets a list of RowDefinition objects defined on this instance of Grid. A list of RowDefinition objects defined on this instance of Grid. The following example creates a Grid with three rows. The Height of the first row is set to the value Auto, which distributes height evenly based on the size of the content that is within that row.
A RowDefinitionCollection for the Grid instance. RowDefinitions is an ordered set of RowDefinition objects that determine the height of each row. Each successive RowDefintion controls the width of each successive row.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With