前端代码:
后台代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
string row = tbRow.Text.ToString();
string column = tbColumn.Text.ToString();
GetButton(row, column);
}
public void GetButton(string column_value, string row_value)
{
int count = mygrid.Children.Count;
for (int i = 0; i < count; i++)
{
Button btn = mygrid.Children[i] as Button;
if (Grid.GetRow(btn) == int.Parse(row_value) && Grid.GetColumn(btn) == int.Parse(column_value))
{
tbResult.Text = btn.Content.ToString();
return;
}
}
}
private Button GetButton(int col, int row)
{
Button b = null;
for (int i = 0; i < grid1.Children.Count; i++)
{
if (grid1.Children[i] is Button)
{
int row1 =(int)(grid1.Children[i] as Button).GetValue(Grid.RowProperty);
int col1 = (int)(grid1.Children[i] as Button).GetValue(Grid.ColumnProperty);
if (col == col1 && row1 == row)
{
b = grid1.Children[i] as Button;
break;
}
}
}
return b;
}
最好是给button名字,找起来方便。
你可以这么试试,先找到所有button,然后判断每个button在哪个“格子”里(在哪行哪列好像是定义在该控件上的,而不是grid中的)