简体   繁体   中英

grid population in xamarin forms

First of all im just starting with xamarin forms, and c# i have managed to use listviews, however im a lil bit confused when it comes to grids, basically what i need is to bind an object to a serie of grids that behave like this:

2 columns per row, i have managed to do it with this code:

<Grid HorizontalOptions="Fill" VerticalOptions="Fill" Margin="10">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="1*" />
            </Grid.ColumnDefinitions>

            <!--fila 1-->
            <StackLayout Grid.Column="0" Grid.Row="0" BackgroundColor="Blue" HorizontalOptions="Fill"></StackLayout>
            <StackLayout Grid.Column="1" Grid.Row="0" BackgroundColor="Red" HorizontalOptions="Fill"></StackLayout>
            <!--fila 2-->
            <StackLayout Grid.Column="0" Grid.Row="1" BackgroundColor="Red" HorizontalOptions="Fill"></StackLayout>
            <StackLayout Grid.Column="1" Grid.Row="1" BackgroundColor="Blue" HorizontalOptions="Fill"></StackLayout>
            <!--fila 3-->
            <StackLayout Grid.Column="0" Grid.Row="2" BackgroundColor="Blue" HorizontalOptions="Fill"></StackLayout>
              <StackLayout Grid.Column="1" Grid.Row="2" BackgroundColor="Red" 
    HorizontalOptions="Fill"></StackLayout>
    </Grid>

在此处输入图片说明

However, i dont know how to fill the grids with information dynamically. with the listview is quite simple since its only with the bind command, here i dont have idea, can someone please point me into the right direction? thanks.

int row = 0;
int col = 0;

// data is a List<string>
foreach (var text in data) {
  var label = new Label() { Text = text };
  grid.Children.Add(box, col, row);

  col++;
  if (col > 1) {
    col = 0;
    row++;
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM