John Livermore
John Livermore

Reputation: 31313

Xamarin Forms - control the height of a button in a grid

Notice the two buttons below. The first is in a StackLayout, and the second is in a 1x1 grid cell. The first obeys the HeightRequest property, but the second will not.

How can I control the size of a Button inside of a GridCell?

enter image description here

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:SimpleApp"
             x:Class="SimpleApp.MainPage">
    <StackLayout>
        <Button Text="Button" 
                HeightRequest="30" 
                HorizontalOptions="Center" />
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="200" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Button Text="Button" 
                    HeightRequest="30"
                    HorizontalOptions="Center" 
                    Grid.Row="0" Grid.Column="0" />
        </Grid>
    </StackLayout>
</ContentPage>

Upvotes: 2

Views: 8159

Answers (2)

Dara Oladapo
Dara Oladapo

Reputation: 596

Alternatively, you can set <RowDefinition Height="200" /> to <RowDefinition Height="Auto" /> if you want to set this to the default button height. as using <RowDefinition Height="200" /> will take any unused space.

Upvotes: 0

Zroq
Zroq

Reputation: 8392

You need to change <RowDefinition Height="200" /> to <RowDefinition Height="*" /> to take in consideration your button size.

If you really want the row to have that size, simply set VerticalOptions="StartAndExpand" in your button XAML code.

Upvotes: 3

Related Questions