我创建了8 * 8个按钮阵列,为Minesweeper Game创建一个网格。创建网格后,我需要访问它们(按钮)。如何访问它们,如更改名称或禁用可点击性或更改颜色。
private void gridDesign()
{
/***********************************Uniform grid*********************/
int firstLoop, secondLoop;
for (firstLoop = 0; firstLoop < 8; firstLoop++)
{
for (secondLoop = 0; secondLoop < 8; secondLoop++)
{
Button lbl = new Button()
{
Name = "_" + firstLoop.ToString() +"_"+ secondLoop.ToString(),
Content = firstLoop.ToString() + " " + secondLoop.ToString(),
};
lbl.Click += button_Click;
uniformgridMinesweeper.Children.Add(lbl);
}
}
/********************************************************************/
}
private void button_Click(object sender, RoutedEventArgs e)
{
Button bbb = e.Source as Button;
bbb.Background = Brushes.Indigo;
//how can i access a button named "_5_5"
}
答案 0 :(得分:0)
使用特定属性(Name)访问Grid的子项是很简单的:
var button = uniformgridMinesweeper.Children.OfType<Button>()
.FirstOrDefault(button => button.Name == "_5_5");
也就是说,您可能需要获取thumbmunkey的建议并将按钮放在您自己的词典或查找中以便快速轻松地访问。或者更好的是,将数据绑定与视图模型一起使用......