1

I have a typical game grid - a UniformGrid consisting of Buttons. The buttons are each data-bound to individual "Cell" objects. Is there a way to access the button itself (and by extension, the particular object that the button is bound to) in the Button's click handler?

user3294629
  • 95
  • 1
  • 7
  • I think this is what you're asking for http://stackoverflow.com/questions/12413985/binding-a-wpf-button-commandparameter-to-the-button-itself-in-datatemplate – kenny Feb 10 '14 at 21:19

1 Answers1

4

If each button handles its own click event then you can use sender that is passed to event handler:

var button = sender as Button;

or if you have one Button.Click handler for all buttons on UniformGrid for example

var button = e.OriginalSource as Button;

and then getting DataContext is as easy as:

var context = button.DataContext;
dkozl
  • 32,814
  • 8
  • 87
  • 89