简体   繁体   中英

Getting index from two-dimentional array by click. WPF

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM