我會解決這個問題(和其他事物一樣)通過數據綁定。
讓我們首先定義我們的視圖模型:
public class ScrabbleViewModel
{
readonly bool[,] matrix = new bool[15,15];
public bool[,] GameMatrix
{
get { return matrix; }
}
}
並將其分配給您的用戶控件或窗口並創建複選框:
public partial class GameWindow : Window
{
public GameWindow()
{
InitializeComponent();
this.DataContext = new ScrabbleViewModel();
CreateCheckBoxes();
}
void CreateCheckBoxes()
{
for(int y=0;y<15;y++)
{
for (int x = 0; x < 15; x++)
{
var chk = new CheckBox();
chk.SetValue(Grid.RowProperty, y);
chk.SetValue(Grid.ColumnProperty, x);
var binding = new Binding(string.Format("GameMatrix[{0},{1}]", y, x));
binding.Mode = BindingMode.TwoWay;
chk.SetBinding(CheckBox.IsCheckedProperty, binding);
grid.Children.Add(chk);
}
}
}
}
您只能自己將得到什麼在你有沒有附加任何東西到你的廣場,確定他們的順序?您可以找到正方形的x/y位置,但這不一定是可靠或可維護的方法。 –