我有几个项目的 ListBox,我需要能够单击它们。问题是,SelectionChanged
当我点击项目的文本时,事件不会被触发,只有当我点击空白部分时。我对 WPF 很陌生,我不明白为什么会这样。
XAML:
<ListBox Name="lBoxVouchers" BorderThickness="0" FontSize="15" SelectionChanged="lBoxVouchers_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem Content="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
处理程序:
private void lBoxVouchers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
MessageBox.Show("You just selected " + e.AddedItems[0]);
}
我正在通过lBoxVouchers.ItemsSource
属性绑定代码中的对象列表,它们会显示出来。当然,每个对象都有一个Name
属性。
我已经尝试在代码和 XAML 中对 ListBox 和项目设置 IsEnabled,但它没有帮助。
也欢迎任何有关在 WPF 中执行此操作的更好方法的评论。