我有一个列表框,其项目模板有一个复选框。 现在,当我单击列表框中的复选框时,它会设置复选框的选中状态。如果我使用键盘“Space”键,我无法更改复选框状态。
注意:键盘是快捷方式,只需将焦点设置到复选框即可。
答案 0 :(得分:2)
如果您不希望ListBox提供选择,您可以使用普通的ItemsControl而不是ListBox:
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
然后你将只有一个CheckBoxes序列而不用ListBoxItem控件包装它们,这些控件将占用键盘焦点。
另一方面,如果您希望ListBox显示选择,那么您可能需要一个多选ListBox,其中CheckBox的状态绑定到ListBoxItem的选定状态。由于单击ListBoxItem将检查CheckBox,因此可以阻止CheckBox集中注意力:
<ListBox ItemsSource="{Binding}" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox
Content="{Binding}"
Focusable="False"
IsChecked="{Binding IsSelected, RelativeSource=
{RelativeSource AncestorType={x:Type ListBoxItem}}}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>