WPF How to know the current button pressed among multiple buttons

You can get the content property using this in your function -

string content = (sender as Button).Content.ToString();

If you place Name or x:Name attributes in your XAML for your buttons, you can then use the native object.Equals() without having to cast or dereference. This also protects you from having to double edit your code, and possibly forgetting to edit in both places when the Content of the control is changed.

Given

<Button Name="btnOne" ... />
<Button Name="btnTwo" ... />

then

if (sender.Equals(btnOne)) {...}
if (sender.Equals(btnTwo)) {...}

Tags:

Wpf

Button