1

我有一个列表(集合),共有 7 个项目,我正在循环以在列表视图中创建一个网格视图。我还有一个检查,例如,如果行数为 5,这将通过向其添加 1 创建一个新列。问题是当行数等于 5 并且为真时,第一列将有 7 个项目,而当创建第二列时,它不会显示剩余的 2 个项目。相反,它再次显示所有 7 个项目。

谁能帮忙,我做错了什么?

foreach (var item in lstOfSoda)
{
    GridLength rowheight1 = new GridLength();
    RowDefinition rowDef1 = new RowDefinition { Height = rowheight1 };
    grdSoda.RowDefinitions.Add(rowDef1);

    GridLength rowheight2 = new GridLength();
    RowDefinition rowDef2 = new RowDefinition { Height = rowheight2 };
    grdSoda.RowDefinitions.Add(rowDef2);

    GridLength columnwidth1 = new GridLength(231);
    ColumnDefinition colDef1 = new ColumnDefinition { Width = columnwidth1 };
    grdSoda.ColumnDefinitions.Add(colDef1);

    GridLength columnwidth2 = new GridLength(5);
    ColumnDefinition colDef2 = new ColumnDefinition { Width = columnwidth2 };
    grdSoda.ColumnDefinitions.Add(colDef2);

    ListView lvSoda = new ListView();
    lvSoda.SetValue(Grid.RowProperty, row + 1);
    lvSoda.SetValue(Grid.ColumnProperty, column);
    grdSoda.Children.Add(lvSoda);

    GridView gridView = new GridView();

    GridViewColumn gvcSodaName = new GridViewColumn();
    gvcSodaName.DisplayMemberBinding = new Binding("SodaName");
    gridView.Columns.Add(gvcSodaName);

    GridViewColumn gvcSodaFlavor = new GridViewColumn();
    gvcSodaFlavor.DisplayMemberBinding = new Binding("SodaFlavor");
    gridView.Columns.Add(gvcSodaFlavor);

    lvSoda.ItemsSource = lstOfSoda;
    lvSoda.View = gridView;
    rowCount++;

    if (rowCount == 5)
    {
        column = column + 2;
        rowCount = 0;
    }
}
4

0 回答 0