-4

On WPF, if I have only one button click event shared for two or more (52 being more precise) is there a way to distinguish which button the event come from?

 private void Button_Card_Click(object sender, RoutedEventArgs e)
        {
            // for testing
            // it works for each button, but which one has been clicked?
            MessageBox.Show("Clicked");
        }

First button object with event set up

Second button object with event set up

Douglas Ferreira
  • 434
  • 1
  • 7
  • 23
  • 1
    Any particular reason why 2 buttons do the same thing? I could understand if one were a menu item –  May 31 '17 at 23:42
  • These buttons are like selectors, click in one of them I would select some data that I'd work on it – Douglas Ferreira May 31 '17 at 23:48
  • Welcome to StackOverflow. A simple google search would have redirect you to older SO question that are _exactly_ the same. In case like this, don't post another question, instead read carefully the answers available on the other questions. – Massimiliano Kraus Jun 01 '17 at 00:03
  • Actually I was expecting an alternative to avoid making 52 methods for each button or 52 if statments. But now I realized maybe I'm using the wrong control for that, the wrong approach. Could I make a list of clickable controls (images, listbox with images) and use a for loop to link objects or variables to them? – Douglas Ferreira Jun 01 '17 at 00:23
  • @DouglasFerreira - Yes, that's a good idea. It helps keep your code strongly-typed. – Enigmativity Jun 01 '17 at 00:23

1 Answers1

1

sender should be the clicked button, but also look at RoutedEventArgs.Source and .OriginalSource

I would also look into using Commands and CommandParameter to indicate which was clicked.

TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47
  • 3
    Don't answer to questions that are _clearly_ duplicates of older questions. Instead flag them as duplicates and move on. Really, we don't need massive duplication of answers on this site. – Massimiliano Kraus Jun 01 '17 at 00:00