【问题标题】:How To Create a Grid inside a Listview Binding Xamarin.Forms如何在 Listview 绑定 Xamarin.Forms 中创建网格
【发布时间】:2015-09-13 03:24:36
【问题描述】:

如何使用数据绑定在 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>

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

提前致谢。

【问题讨论】:

    标签: c# listview xamarin xamarin.forms


    【解决方案1】:

    您也可以使用下一个方法示例(通过 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>
    

    【讨论】:

      【解决方案2】:

      在 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;
              });
      

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

      【讨论】:

        【解决方案3】:

        如果您想动态添加行和列,您可以在page.cs 中创建一个网格并将其绑定到page.xaml。假设您有一组项目,并且您想在网格中对它们进行排序:

        public page()
        {
            string[] items = {"Item 1","Item 2",......."Item n"};
            var itemsGrid = new Grid();
        
            int k = 1 + items.Length / 3;
            // just to make it clear in the for loop i used (int k)  
            // suppose 3 column need we divide on 3
        
            // define the number of rows according to the number of item you have
            for (int i = 0; i <k ; i++)
            {
                    itemsGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
            }
        
            // defining column number (in this case 3)
            for (int j = 0; j < 3; j++)
            {
                    itemsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
            }
        
            // adding the items to the grid (3 column , RN rows)
            int RN = 0;  // initializing the row number 
        
            for (int num = 0; num < items.Length; num++)
            {
                    if (num % 3 == 0)     // first column
                    {
                        itemsGrid.Children.Add(new Label()  // adding the item as label
                        {
                            Text = items[num],
                            TextColor = Color.White,
                            BackgroundColor = Color.Blue,
                        HorizontalOptions = LayoutOptions.FillAndExpand,
                        HorizontalTextAlignment = TextAlignment.Center,
                        VerticalTextAlignment = TextAlignment.Center
                    }, 0, RN);
        
                    }
                    else if (num % 3 == 1)// second column
                    {
                        itemsGrid.Children.Add(new Label()
                        {
                            Text = items[num],
                            TextColor = Color.White,
                            BackgroundColor = Color.Blue,
                            HorizontalOptions = LayoutOptions.FillAndExpand,
                            HorizontalTextAlignment = TextAlignment.Center,
                            VerticalTextAlignment = TextAlignment.Center
                        }, 1, RN);
                    }
                    else  //third column
                    {
                        itemsGrid.Children.Add(new Label()
                        {
                            Text = items[num],
                            TextColor = Color.White,
                            BackgroundColor = Color.Blue,
                            HorizontalOptions = LayoutOptions.FillAndExpand,
                            HorizontalTextAlignment = TextAlignment.Center,
                            VerticalTextAlignment = TextAlignment.Center
                        }, 2, RN);
                        RN++;
                    }
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-08-05
          • 1970-01-01
          • 2015-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-22
          • 2014-10-03
          相关资源
          最近更新 更多