1

I wonder if there is any way to get index from 2-dimentional array by this way in WPF.

bt[i, j].Click += Button_Click;

private void Button_Click(object sender, RoutedEventArgs e)
{
    (sender as Button) ???
}

How can i get i,j of button that i clicked?

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Zackk
  • 73
  • 2
  • 9

1 Answers1

0

There's a few ways.

If the button click handler is in the same class as the buttons could you search for it?

for (var i = 0; i < count; i++)
  for (var j = 0; j < count; j++)
    if (bt[i, j] == sender)
    {
      // found it
    }

Or you can set the Tag of the button.

bt[i, j].Click += Button_Click;
bt[i, j].Tag = Tuple.Create(i, j);

Another is to create a closure for the click handler.

private Action<object, RoutedEventArgs> Button_Click(int i, int j)
{
    return (object sender, RoutedEventArgs e) =>
    {
        (sender as Button) ???
    };
}

bt[i, j].Click += Button_Click(i, j);
Cameron MacFarland
  • 70,676
  • 20
  • 104
  • 133