How to get sender in click handler from toolbartray or other control in wpf?

XAML:

<ToolBarTray Name="tlbTray" ButtonBase.Click="tlbTray_Click">

<ToolBar Name="tlbFile">
    <Button Name="btnOpen"><Image Source="images\folder.png" Stretch="None" /></Button>
    <Button Name="btnSave"><Image Source="images\disk.png" Stretch="None" /></Button>
</ToolBar>

</ToolBarTray>

      

code:

private void tlbTray_Click(object sender, RoutedEventArgs e)
{
  // How to get the name of the button or control that triggered the event
}

      

As commented in the method. How to get the name of the button or control that raised the event. Or am I doing it wrong? I just want to route all click events to this method and decide what to do from there ...

Thank!; -)

+1


source to share


2 answers


Owkay, I found it!



private void tlbTray_Click(object sender, RoutedEventArgs e)
{
  Button button = (Button)e.OriginalSource;
}

      

+2


source


In your handler, use:



Button test = (Button)sender;
if(test.Name=="btnOpen")
{
  //Do something
}

      

+1


source







All Articles