动态地将BoxView添加到网格

我试图使用3列和多行以网格格式添加BoxView。 我已经使用xaml和c#文件中的行为定义了网格。 应该发生的是应该为相同数量的图像创建BoxView,每列3个图像。

谢谢,

XAML

             

C#

 public MainPage() { InitializeComponent(); int colMaximum = 3; int numberOfImages = 15; //To add three columns for (int i = 0; i < colMaximum; i++) { imageGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(120, GridUnitType.Absolute) }); } //To add an array of rows imageGrid.RowDefinitions = new RowDefinitionCollection(); for (int myCount = 0; myCount <= numberOfImages / colMaximum; myCount++) { imageGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(120, GridUnitType.Absolute) }); //To add a new box view for each for (int newcol = 0; newcol <= colMaximum; newcol++) { for (int newrow = 0; newrow <= numberOfImages / colMaximum; newrow++) { imageGrid.Children.Add(new BoxView() { BackgroundColor = Color.Red }); } } } } 

将子项添加到网格时,必须指定Row和Col,否则它们将添加到0,0。

  imageGrid.Children.Add(new BoxView() { BackgroundColor = Color.Red }, newrow, newcol); 
Interesting Posts