add elements to WPF Grid

Posted by Konrad on Stack Overflow See other posts from Stack Overflow or by Konrad
Published on 2010-04-05T10:53:51Z Indexed on 2010/04/05 11:03 UTC
Read the original article Hit count: 439

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf