tags:

views:

151

answers:

1

I wanted to make a function that populates a Grid in WPF with pictures. So I did that:

private void setCellImage(Grid g, Image img, int column, int row)
{

    Grid.SetColumn(img, column);
    Grid.SetRow(img, row);

    if (!g.Children.Contains(img))
        g.Children.Add(img);

    g.UpdateLayout();
}

And was using it by calling in that way:

for (int i = 0; i < 15; i++)
    for(int j=0; j<15; j++)
        setCellImage(gameMap,background, i, j);

But it wasn't working. it populated a grid only in cell 14,14 leaving all other cells blank.

I thought that it may be my mistake that I should use another instances of Image but it wasn't that:

private  void setCellImage(Grid g, Image img, int column, int row)
{
    Image _img = new Image();
    _img = img;

    Grid.SetColumn(_img, column);
    Grid.SetRow(_img, row);

    if (!g.Children.Contains(_img))
        g.Children.Add(_img);

    g.UpdateLayout();
}

This thing is still not working.

+1  A: 

Where is the value of the background variable set? If it is not going to change in your loop then you're just displacing the same image here and there

Veer
Both of you were right. I made modifications and it is now working just great. Thank you!
Konrad