tags:

views:

338

answers:

2

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 do I get the name of the button or control that triggered the event. Or I'm handling this the wrong way? I just want to route all click events to that one method and decide what to do from there...

Thanks! ;-)

A: 

In your handler use:

Button test = (Button)sender;
if(test.Name=="btnOpen")
{
  //Do something
}
Nate
Actually no... The sender isn't the button but tlbTray in wpf apps. That's exactly the reason I asked this question. I don't see where i can get the original sender.
Sander Versluys
well I learn something new everyday
Nate
A: 

Owkay, I found it!

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