Xamarin Forms Grid - Row height of "*" in C#?

24,870
var grid = new Grid ();
grid.RowDefinitions.Add (new RowDefinition { Height = GridLength.Auto });
grid.RowDefinitions.Add (new RowDefinition { Height = new GridLength (1, GridUnitType.Star) });

var stacklayout1 = new StackLayout { HeightRequest = 100, BackgroundColor = Color.Red };
var stacklayout2 = new StackLayout { BackgroundColor = Color.Blue };

Grid.SetRow (stacklayout2, 1);

grid.Children.Add (stacklayout1);
grid.Children.Add (stacklayout2);

MainPage = new ContentPage { Content = grid };  

Screenshot of the above layout on iOS

Share:
24,870
Ilia Stoilov
Author by

Ilia Stoilov

I'm a Xamarin developer currently building a company mainly dedicated to creating mobile applications.

Updated on July 09, 2022

Comments

  • Ilia Stoilov
    Ilia Stoilov almost 2 years

    I know that you can set the row height with a "*" in XAML this way:

     <RowDefinition Height="Auto" />
     <RowDefinition Height="*" />
    

    but the same expression in C# returns an error:

    new RowDefinition { Height = new GridLength("*", GridUnitType.Auto) },
    

    So my question is how to set the row height of a grid to a "*" in C#?

  • Ilia Stoilov
    Ilia Stoilov almost 9 years
    Thanks, and how can I make the last row to extend to the bottom of the layout no matter what the content of the row is?
  • acido
    acido over 6 years
    I do have the same question as @IliaStoilov, Did you solve it?
  • Ilia Stoilov
    Ilia Stoilov over 6 years
    Just set the GridUnitType of the row to Auto and set the VerticalOptions of the object you place in the grid to FillAndExpand.