How to setup Row/Column Definitions in Xamarin.Forms Xaml?

23,966

I also get the same behaviour (does not fill and expand) for a grid with a single cell (1 row / column, although I am not sure why we would ever need a grid with a single cell full size of screen), but it seems to work fine for a 2 x 2, 3 x 3 cell grid (have not tried others yet).

The Height="" and Width="" attributes are required in Xamarin Forms although I "think" they are not needed in WPF as the default assumes this is the case.

 <ContentPage.Content>
    <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
    <Grid.RowDefinitions>
    <RowDefinition Height="*"></RowDefinition>
    <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*"></ColumnDefinition>
    <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <Button Grid.Row="0" Grid.Column="0" Text="1"></Button>
    <Button Grid.Row="1" Grid.Column="1" Text="2"></Button>
    </Grid>
</ContentPage.Content>
Share:
23,966
Alexey Strakh
Author by

Alexey Strakh

Updated on September 29, 2020

Comments

  • Alexey Strakh
    Alexey Strakh over 3 years

    I managed to setup rows and columns from the code but couldn't move this settings to xaml:

    grid.RowDefinitions = new RowDefinitionCollection { 
        new RowDefinition { Height = new GridLength(1, GridUnitType.Star) } 
    };
    grid.ColumnDefinitions = new ColumnDefinitionCollection { 
        new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) } 
    };
    

    The following doesn't work:

    <Grid x:Name="grid" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
        <Grid.RowDefinitions>
          <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        ...
    </Grid>
    

    From the documentation I managed to fetch only c# implementation