我有一个列表(集合),共有7个项目,我正在循环以在listview中创建gridview。我还检查一下,例如,如果行数为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;
}
}