WP7 Silverlight grid not showing content

2019-09-04 07:00发布

It's been 2 hours of struggle against SL grid on WP7. I build my grid using the following code:

public void initUIBoard() { int x, y; Button b;

        for (x = 0; x < mine.cRows; x++) {
            RowDefinition rd = new RowDefinition();
            rd.Height = new GridLength(20);
            uiBoard.RowDefinitions.Add(rd);
        }


        for (y = 0; y < mine.cColumns; y++) {
            ColumnDefinition cd = new ColumnDefinition();
            cd.Width = new GridLength(20);
            uiBoard.ColumnDefinitions.Add(cd);
        }


        for (x = 0; x < mine.cRows; x++)
            for (y = 0; y < mine.cColumns; y++)
            {
                b = new Button();
                b.Click += new RoutedEventHandler(this.caseClick);

                b.Tag = mine.gameBoard[x][y];
                Grid.SetRow(b, x);
                Grid.SetColumn(b, y);

                uiBoard.Children.Add(b);
            }

    }

The thing is, my grid is shown empty, am I doing something wrong with these Rows/Columns definition or something ?

Thanks in advance

2条回答
淡お忘
2楼-- · 2019-09-04 07:27

After some experimentation, it looks like GridLength isn't calculating heights in pixels correctly.
Because the grid cell created isn't large enough the control is not shown.

Try increasing the sizes used for grid length. I did the following and got some output.

rd.Height = new GridLength(40);

Alternatively, consider setting the Heights and Widths to by dynamically sized. e.g.:

rd.Height = new GridLength(1, GridUnitType.Auto);

If you can investigate this height issue some more and also find it to be a height issue bug then please submit it to Microsoft.

查看更多
做自己的国王
3楼-- · 2019-09-04 07:46

Your code seems to work fine (I tried on Silverlight non-Winphone, but should be the same).

My guess is that the cause is somewhere else, for ex. another element covering the uiBoard grid, or the buttons being transparent with no background color/border.

查看更多
登录 后发表回答