I have a ListBox, that's bound to an ObservableCollection
.
Each ListBoxItem
is displayed with a DataTemplate
. I have a button in my DataTemplate
, that when clicked, needs a reference to the member of the ObservableCollection
it's part of the DataTemplate for. I can't use the ListBox.SelectedItem
property because the item does not become selected when clicking the button.
So either: I need to figure out how to properly set ListBox.SelectedItem
when the mouse hovers, or when the button is clicked. Or I need to figure out another way to get a reference to the CLR Object bound to the ListBoxItem
that the button belongs to. The second option seems cleaner, but either way is probably OK.
Simplified code segment below:
XAML:
<DataTemplate x:Key="postBody">
<Grid>
<TextBlock Text="{Binding Path=author}"/>
<Button Click="DeleteButton_Click">Delete</Button>
</Grid>
</DataTemplate>
<ListBox ItemTemplate="{StaticResource postBody}"/>
C#:
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("Where mah ListBoxItem?");
}