键盘焦点到WPF中的列表框项目 [英] Keyboard focus to list box items in WPF

查看:105
本文介绍了键盘焦点到WPF中的列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框,其项目模板有一个复选框。
现在,当我单击列表框中的复选框时,它设置复选框的选中状态。如果我使用键盘空格键,我不能改变复选框的状态。

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 sets 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.

推荐答案

如果你不想让ListBox提供选择,你可以使用一个普通的ItemsControl而不是ListBox:

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>

然后你将只有一个CheckBox的序列,而不用ListBoxItem控件, 。

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

另一方面,如果你想ListBox显示选择,那么也许你想要一个多选择ListBox,其中CheckBox的状态绑定到选定的状态的ListBoxItem。因为点击ListBoxItem将会检查CheckBox,所以你可以防止CheckBox被关注:

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>

这篇关于键盘焦点到WPF中的列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆