如何在Listview绑定Xamarin.Forms中创建网格

Atu*_*uka 6 c# listview xamarin xamarin.forms

如何在ListView中使用数据绑定创建Grid?我正在用Xamarin.Forms创建这个应用程序.

如果我不知道我需要多少行和列,如何在ListView绑定中动态创建Grid?

这是我到目前为止:

<ListView x:Name="List" HasUnevenRows="True">
  <ListView.ItemTemplate>
   <DataTemplate>
    <ViewCell>
      <ViewCell.View>
        <Grid Padding="10,10,10,10">
          <Grid.RowDefinitions>
            <RowDefinition Height="200"></RowDefinition>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition  Width="200"></ColumnDefinition>
          </Grid.ColumnDefinitions>
          <StackLayout BackgroundColor="#313FA0" Grid.Row="0" Grid.Column="0" HeightRequest="200" WidthRequest="200">
            <Label Text="{Binding NUMBER}" FontSize="50" TextColor="White" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
            <Label Text="{Binding NAME}" FontSize="30" TextColor="White" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
          </StackLayout>
        </Grid>
      </ViewCell.View>
    </ViewCell>
   </DataTemplate>
  </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

在此代码中,只创建了一行和一列.如果我有多个数据点,我该如何解决这个问题?例如,如果我需要一行有两列.

提前致谢.

Eli*_* MP 5

您也可以使用下一个方法示例(通过 xaml,前端)。

<ContentPage.Resources>
      <ResourceDictionary>
        <Color x:FactoryMethod="FromHex" x:Key="fondoBlancoPalido">
          <x:Arguments>
            <x:String>#F2F2F2</x:String>
          </x:Arguments>
        </Color>
      </ResourceDictionary>
    </ContentPage.Resources>

<ListView x:Name="listView" HasUnevenRows="True"  ItemsSource="{Binding .}" BackgroundColor="{StaticResource fondoBlancoPalido}">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <ViewCell.View>
                <Grid Padding="5">
                  <Grid.RowDefinitions>
                    <RowDefinition Height="60"></RowDefinition>
                    <RowDefinition Height="60"></RowDefinition>
                    <RowDefinition Height="10"></RowDefinition>
                  </Grid.RowDefinitions>
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*"></ColumnDefinition>
                    <ColumnDefinition Width="2*"></ColumnDefinition>
                    <ColumnDefinition Width="3*"></ColumnDefinition>
                  </Grid.ColumnDefinitions>

                  <Button Grid.Row="0" Grid.Column="0" Clicked="OnStartClicked" Image="play.png" BackgroundColor="Transparent" HorizontalOptions="Center" Grid.RowSpan="2"/>
                  <Label Grid.Row="0" Grid.Column="1" Text="Hora de Inicio: " XAlign="Center" YAlign="Center" TextColor="Black" FontAttributes="Bold"/>
                  <Label Grid.Row="0" Grid.Column="2" Text="{ Binding attribute3 }" XAlign="Center" YAlign="Center" TextColor="Black"/>
                  <Label Grid.Row="1" Grid.Column="1" Text="Encargado de la Tarea: " XAlign="Center" YAlign="Center" TextColor="Black" FontAttributes="Bold"/>
                  <Label Grid.Row="1" Grid.Column="2" Text="{ Binding attribute4 }" XAlign="Center" YAlign="Center" TextColor="Black"/>
                  <BoxView Color="Navy" HeightRequest="2" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3"/>
                </Grid>
              </ViewCell.View>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
Run Code Online (Sandbox Code Playgroud)


Joe*_*air 4

没有一种好方法可以在 XAML 中动态构建具有可变数量的行或列的网格布局。我建议在代码隐藏文件中创建 DataTemplate,您可以在其中轻松添加所需数量的 RowDefinitions 和 ColumnDefinitions。这是一个例子:

        var myDataTemplate = new DataTemplate(() =>
        {
            var cell = new ViewCell();
            var grid = new Grid();

            foreach (var record in myRecords)
            {
                grid.RowDefinitions.Add(new RowDefinition());
            }

            foreach (var field in myFields)
            {
                grid.ColumnDefinitions.Add(new ColumnDefinition());
            }

            /*
             * 
             * Populate grid here...
             * 
             */

            cell.View = grid;
            return cell;
        });
Run Code Online (Sandbox Code Playgroud)

然后只需将此 DataTemplate 分配给您的 ListView。