我有一个未知数量的图像,我用代码添加到了一个Grid控件中,当图像以错误的顺序插入时,我有点迷失在逻辑中。看一看(由于测试,模量如下):
grid.Height = this.Height;
grid.Width = this.Width;
grid.ShowGridLines = true;
for (int i = 0; i < 50; i++)
{
RowDefinition rowDef = new RowDefinition();
rowDef.Height = new GridLength(50);
grid.RowDefinitions.Add(rowDef);
ColumnDefinition colDef = new ColumnDefinition();
colDef.Width = new GridLength(50);
grid.ColumnDefinitions.Add(colDef);
}
int x = 1;
int y = 1;
for (int i = 0; i < 50; i++)
{
y++;
if (i % 10 == 0)
{
x++;
y = 1;
}
Image img = new Image() { Source = new BitmapImage(new Uri("Images/positive.png",UriKind.Relative)), Width = 50, Height = 50, Margin = new Thickness(2,2,2,2) };
grid.Children.Add(img);
Grid.SetRow(img, x);
Grid.SetColumn(img, y);
}
结果:
正如你所看到的,这些图像按照模数所说的每10个图像开始一行,但它们不会从第一列的第一行开始。
我想实现的是:
我做错了什么?谢谢