Catching events from Controls in DataTemplate

I think my question is pretty simple. I'm still getting my head wrapped around WPF, but I have the following situation I simply can't figure out how to solve:

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.

[3808 byte] By [theblueeyz] at [2008-1-8]
# 1

Take to e.Source (which is your Button) and use the VisualTreeHelper to walk up the VisualTree until you find your ListboxItem.

Martin_Moser at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2
Works beautifully. Thanks!
theblueeyz at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3

I just wanted to add -

Another approach I tried, which ended up to be easier because of the complex relationship between the view elements, was simply to assign the Tag element on the ListBoxItem's button to the object being bound, and then do an appropriate cast in my code behind:

<Button Tag="{Binding}" .... etc ... >

Retrieving the Tag property then gives you the bound object, and then I just set the SelectedValue on the ListBox. That turned out to be much easier.

theblueeyz at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4

Cast the DataContext of the button, instead of using Tag.

Anthony_Sebastian at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5
Any particular reason to use DataContext rather than Tag? I guess it is more convenient (and semantically correct, since you don't have to explicitly set the Tag).
theblueeyz at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 6

The datacontext is already set, so there's no reason to use the tag. Although the tag does work fine, until you need it for something else.

Thanks,

Anthony.

Anthony_Sebastian at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified