How to enable borders in Grid in Xamarin.Forms

66,563

Solution 1

There's no Border property for GridView, but:

Just set grid.BackgroundColor to your desired border color value, then set grid.ColumnSpacing and grid.RowSpacing to some value and make sure all controls you add to the grid have own BackgroundColor set correctly.

Solution 2

Here is the full answer (in XAML) without needing to write a custom renderer or Effect.

The code is little verbose but easy to understand and the result is like on the image

enter image description here

Here is the code to put the borders on your grid (and whats more you´ll have total control over them like you notice there is no blue line on the far left)

<Grid BackgroundColor="White">
  <Grid.RowDefinitions>
    <RowDefinition Height="1"/>
    <RowDefinition Height="15"/>
    <RowDefinition Height="1"/>
    <RowDefinition Height="15"/>
    <RowDefinition Height="1"/>
    <RowDefinition Height="15"/>
    <RowDefinition Height="1"/>
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto"  />
    <ColumnDefinition Width="1" />
    <ColumnDefinition Width="10" />
    <ColumnDefinition Width="1" />
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="1" />
    <ColumnDefinition Width="50" />
    <ColumnDefinition Width="1" />
  </Grid.ColumnDefinitions>
  <BoxView Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="8" BackgroundColor="Red" HeightRequest="1" VerticalOptions="End" HorizontalOptions="FillAndExpand"/>
  <!--Your stuff here!-->
  <BoxView Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="8" BackgroundColor="Red" HeightRequest="1" VerticalOptions="End"  HorizontalOptions="FillAndExpand"/>
   <!--Your stuff here!-->
  <BoxView Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="8" BackgroundColor="Red" HeightRequest="1" VerticalOptions="End"  HorizontalOptions="FillAndExpand"/>
   <!--Your stuff here!-->
  <BoxView Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="8" BackgroundColor="Red" HeightRequest="1" VerticalOptions="End"  HorizontalOptions="FillAndExpand"/>

  <!--Vertical lines and no "stuff"-->
  <BoxView Grid.Column="1" Grid.Row="0" Grid.RowSpan="7"  BackgroundColor="Blue" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="End"/>
  <BoxView Grid.Column="3" Grid.Row="0" Grid.RowSpan="7"  BackgroundColor="Blue" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="End"/>
  <BoxView Grid.Column="5" Grid.Row="0" Grid.RowSpan="7"  BackgroundColor="Blue" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="End"/>
  <BoxView Grid.Column="7" Grid.Row="0" Grid.RowSpan="7"  BackgroundColor="Blue" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="End"/>
</Grid>

Solution 3

enter image description here

  <Grid BackgroundColor="White" >
        <BoxView BackgroundColor="Pink"  />
        <Grid BackgroundColor="White" Margin="5">

        </Grid>
    </Grid>

Solution 4

Just noticed my example is similar to Sturla's but a little different so I will leave it up.

The code is not super pretty but I did something similar by adding a 1px BoxView between each column and then 1 on top of your Grid and one on the bottom of your Grid, like so:

<Grid VerticalOptions="FillAndExpand"
      HorizontalOptions="FillAndExpand"
      RowSpacing="0"
      ColumnSpacing="0">

  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*"/>
  </Grid.ColumnDefinitions>

  <Grid.RowDefinitions>
    <RowDefinition Height="1"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="1"/>
  </Grid.RowDefinitions>

  <BoxView BackgroundColor="Black"
           HeightRequest="1"
           HorizontalOptions="FillAndExpand"
           Grid.Row="0"/>

  <Grid VerticalOptions="Start"
        ColumnSpacing="0"
        Grid.Row="1">

    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="1"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Button Text="Button 1"/>

    <BoxView BackgroundColor="Black"
             WidthRequest="1"
             VerticalOptions="FillAndExpand"
             Grid.Column="1"/>

    <Button Text="Button 1"
            Grid.Column="2"/>
  </Grid>

  <BoxView BackgroundColor="Black"
           HeightRequest="1"
           HorizontalOptions="FillAndExpand"
           Grid.Row="2"/>
</Grid>

*Edit: Since writing this, I have changed the way I do it a bit. Now, like Daniel Luberda's answer, I simply set the Grid.BackgroundColor to Color.Black and then I can remove all of the BoxViews and I am done. I do this because I assume it is much better to have few views on the screen, especially if you are putting something like the above in a ListView.

Also, since a lot of my pages will animate the Buttons when the page load (using ScaleTo()) I initially set the Grid.BackgroundColor to Color.Transparent or Color.White and then once the animation is done, I change it to Color.Black. Has worked pretty well so far.

Solution 5

If you would like a solution with more equal borders than Daniel Luberda'S anwser, here's what i used :

Make a Grid in which you want element to have borders. Put the spacing between colomns and rows to 0. For each element of the Grid make another Grid with a Boxview in it, and your view on top of that Boxview. Then,put each BoxView to fill and expand. Then adjust the padding of these "under"-Grids as you'd like. Each element of your grid will be separated equaly.

This is pretty heavy though.

Share:
66,563
Shaun Cheng
Author by

Shaun Cheng

Passionate and experienced Full Stack expert in web and mobile application development. ★ Javascript/MERN/MEAN expert - React/Redux, Angular, TypeScript, Node.js, MongoDB, Vue.js, ES2017/2016, Webpack ★ LAMP Stack Web Developer - HTML5, CSS3, ROR Bootstrap, jQuery, Wordpress, Laravel, CodeIgnitor, Ajax, XML ★ Pro Mobile Expert - iOS, Android, Swift, Kotlin, Xamarin, React Native, Java

Updated on February 12, 2020

Comments

  • Shaun Cheng
    Shaun Cheng over 4 years

    I'm building a grid in Xamarin.Forms. And I'd like to add borders like tables. I thought that I could add the border when defining rows and columns, but failed. Can anyone help me? This is my current code.

    Grid grid = new Grid {
        VerticalOptions = LayoutOptions.FillAndExpand,
        RowDefinitions = {
            new RowDefinition { Height = GridLength.Auto },
            new RowDefinition { Height = GridLength.Auto },
            new RowDefinition { Height = GridLength.Auto },
            new RowDefinition { Height = GridLength.Auto },
            new RowDefinition { Height = GridLength.Auto },
            new RowDefinition { Height = GridLength.Auto },
            new RowDefinition { Height = GridLength.Auto },
            new RowDefinition { Height = GridLength.Auto },
            new RowDefinition { Height = GridLength.Auto },
            new RowDefinition { Height = GridLength.Auto },
            new RowDefinition { Height = GridLength.Auto },
    
        },
        ColumnDefinitions = {
            new ColumnDefinition { Width = new GridLength (1, GridUnitType.Star) },
            new ColumnDefinition { Width = new GridLength (5, GridUnitType.Star) },
            new ColumnDefinition { Width = new GridLength (1, GridUnitType.Star) },
        }
    };