Catching events from Controls in DataTemplate
I have a ListBox I am using to present a list of objects.
I have created the following DataTemplate for the objects in the list:
<DataTemplate x:Key="ItemDataTemplate">
<Border x:Name="tplBorder">
<Grid>
<Button Content="{Binding Path=Name, Mode=OneWay}"/>
<Image Source="{Binding Path=SomeProperty, Converter={StaticResource ImageConv}, Mode=OneWay}"/>
</Grid>
</Border>
</DataTemplate>
Easy. Now, in order to catch events from any button in any ListBox item, I do the following to the ListBox when I declare it in my main window's XAML:
<ListBox ItemsSource="{Binding Source={StaticResource MyDataSource}}" x:Name="myList" Button.Click="ListboxItemButtonClick"/>
Ok, so then I have a corresponding event handler in my C# code:
privatevoid ListboxItemButtonClick(object sender,RoutedEventArgs e)
{
Console.WriteLine(e.Handled.ToString());
Console.WriteLine(e.OriginalSource.ToString());
Console.WriteLine(e.RoutedEvent.ToString());
Console.WriteLine(e.RoutedEvent.Name.ToString());
Console.WriteLine(e.RoutedEvent.OwnerType.ToString());
Console.WriteLine(e.RoutedEvent.RoutingStrategy.ToString());
Console.WriteLine(e.Source.ToString());
}
And as expected, what I see there indicates that a Button is clicked, and the event bubbles up through the ListBox, and I receive the event from the ListBox.
Now, here's my actual question:
How do I tell which button belonging to which ListBoxItem was actually clicked? What I want to do is set the item containing the clicked button to be the selected item on the ListBox.

