开发者

Keyboard focus to list box items in WPF [duplicate]

This question already has answers here: WPF ListBoxItem selection problem (3 answers) Closed 8 years ago.

I am having a list box, and its item template is having one check box. Now, when I click on the check box in the listbox, it s开发者_运维问答ets the checked status of the check box. If I use keyboard "Space" key, I cannot change the checkbox state.

Note: Keyboard is shortcut is working once I set focus to the check box by clicking it.


If you don't want the ListBox to provide selection at all, you could use a plain ItemsControl instead of a ListBox:

<ItemsControl ItemsSource="{Binding}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Then you will just have a sequence of CheckBoxes without wrapping them with ListBoxItem controls that will take the keyboard focus.

On the other hand, if you want the ListBox to display selection, then maybe you want a multi-select ListBox where the state of the CheckBox is bound to the selected state of the ListBoxItem. Since clicking the ListBoxItem will check the CheckBox, you could prevent the CheckBox from being focused at all:

<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜