I want to say "no, it's impossible", but it cannot be serious, right? :-)
I have no idea how you created the array of buttons, but it's pretty obvious that you need an array of them. I would suggest you don't use XAML for that, because XAML would create individual buttons, not the array. However, I would suggest you create an instance of
Grid
in XAML and fill it in code behind. Doing it in XAML means to much of dumb manual work which is hard to maintain.
const int matrixSize = 5;
Grid buttonGrid =
Button[,] buttons = new Button[matrixSize, matrixSize];
for (int yIndex = 0; yIndex < matrixSize; ++yIndex)
for (int xIndex = 0; xIndex < matrixSize; ++xIndex) {
buttons[yIndex, xIndex] = new Button();
buttonGrid.SetRow(buttons[yIndex, xIndex], yIndex);
buttinGrid.SetColumn(buttons[yIndex, xIndex], xIndex);
}
Using
Grid.SetRow
and
Grid.SetColumn
methods is the key; this is the way to set an
attached property to an instance of a
UIElement
:
https://msdn.microsoft.com/en-us/library/ms589026%28v=vs.110%29.aspx[
^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.grid_properties%28v=vs.110%29.aspx[
^].
See also:
https://msdn.microsoft.com/en-us/library/vstudio/ms749011%28v=vs.100%29.aspx[
^],
https://msdn.microsoft.com/en-us/library/vstudio/ms752914%28v=vs.100%29.aspx[
^].
If you do it this way, you can use the similar loop any time later to change any other properties.
—SA