Reputation: 25726
I have a ListBox
with a rather simple ItemTemplate
defined - containing a TextBlock
and a Button
. This displays like expected, but there is a problem though. When I click the content of the ListBoxItem
, i.e. the text or the button, the line doesn't get selected in the ListBox
. If I click the blank parts of the line it does. I'd like the ListBoxItem
to be selected when I click anywhere on the line. What's needed to achieve this?
<ListBox ItemsSource="{Binding SomeElements}">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem Selected="ListBoxItem_Selected">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}"></TextBlock>
<Button>Click</Button>
</StackPanel>
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Upvotes: 4
Views: 1411
Reputation: 39956
@Natrium Nope, the problem is different here,
ListBoxItem DataTemplate ListBoxItem StackPanel...
Change your code to this.
<ListBox ItemsSource="{Binding SomeElements}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}"></TextBlock>
<Button>Click</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Upvotes: 4