WPF How to know the current button pressed among multiple buttons

前端 未结 4 1873
北荒
北荒 2020-12-15 13:24

I am having multiple buttons with contents 1, 2, 3, 4, 5... like this. All buttons are using same function on Click event.

4条回答
  •  佛祖请我去吃肉
    2020-12-15 14:02

    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

    then

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

提交回复
热议问题