2011-10-20 99 views
0

想象下面的場景(Silverlight 4)。我有兩個矩形。一個是黑色的,另一個是白色的。他們都有固定的大小,比如50x50。我也有,我想,以填補這些矩形,但以交替方式的區域(500×500) - 白色,黑色,白色,黑色等。爲了更好地說明,看看鏈接波紋管:動態生成n(行)m(列)棋盤

http://screencast.com/t/BwsPSbtg2eaM

http://screencast.com/t/gTuexSSyW

視頻(鏈接#2)演示了我正在努力實現的目標。

任何幫助將不勝感激!

瓊斯

回答

1
int totalRectsInaRow = TotalWidth/ WidthOfOneRect; 
    int totalRectsInaColumn = TotalHeight/ HeightOfOneRect; 

    //Create a Grid of Width = TotalWidth and Height = Total Height; 
    //Add columns equal to totalRectsInaColumn and rows equal to totalRectsInaRow in Grid 
    //Set wdith of each column equal to width of one rectangle 
    //set height of each row equal to height of one rectangle 

    bool drawWhite = true; 
    for (int i = 0; i < totalRectsInaColumn; i++) 
    { 
     for (int j = 0; j < totalRectsInaRow; j++) 
     { 
      if (drawWhite) 
      { 
       //draw white rectanlge at i column and j row 
       //basically you create a rectangle and place it in grid on particular location 
       DrawWhileRectangle(i, j); 
       drawWhite = false; 
      } 
      else 
      { 
       //draw black rectanlge at i column and j row 
       //basically you create a rectangle and place it in grid on particular location 
       DrawBlackRectangle(i, j); 
       drawWhite = true; 
      } 
     } 
     drawWhite = !drawWhite; 
    } 
+0

謝謝...我正在尋找一個更XAML的做法。 –