I have a ListBox for a few items, and I need to be able to click them. Problem is, the SelectionChanged
event doesn't get fired when I click on the item's text, only if I click on the blank part. I'm quite new to WPF, and I don't understand why this is happening.
XAML:
<ListBox Name="lBoxVouchers" BorderThickness="0" FontSize="15" SelectionChanged="lBoxVouchers_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem Content="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Handler:
private void lBoxVouchers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
MessageBox.Show("You just selected " + e.AddedItems[0]);
}
I'm binding the list of objects in code via the lBoxVouchers.ItemsSource
property, and they show up. Each object has a Name
property, of course.
I've tried setting IsEnabled on the ListBox and the items, both in code and XAML, but it doesn't help.
Any comments about better ways to do this in WPF are also welcome.