Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 859
Alexey Strakh Avatar asked Jun 19 '14 19:06

Alexey Strakh


1 Answers

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>
like image 91
WickedW Avatar answered Sep 19 '22 17:09

WickedW