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

时间:2015-06-26 08:42:23

标签: 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>

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

提前致谢。

3 个答案:

答案 0 :(得分:4)

在XAML中动态构建具有可变行数或列数的Grid布局没有好办法。我建议在代码隐藏文件中创建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。

答案 1 :(得分:3)

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

如果要动态添加行和列,可以在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++;
            }
        }
    }