WPF 列表框和全选 [英] WPF Listbox and Select All

查看:66
本文介绍了WPF 列表框和全选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的 ListBox 并将 SelectAll 作为上下文菜单项.但是,似乎 ListBox 对 SelectAll 有某种内置处理,但我无法正常工作,但干扰了我实现 SelectAll 的尝试.

I want to create a simple ListBox and have SelectAll as a context menu item. However it seems that ListBox has some sort of inbuilt handling for SelectAll that I can't get working, but is interfering with my attempt to implement SelectAll.

我的整个 XAML 是这样的:

My entire XAML is this:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.SelectAll"
                        Executed="SelectAllExecuted" />
    </Window.CommandBindings>
    <DockPanel>
        <CheckBox DockPanel.Dock="Top">My Checkbox</CheckBox>
        <ListBox Name="listBox" SelectionMode="Multiple">
            <ListBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Command="ApplicationCommands.SelectAll" />
                </ContextMenu>
            </ListBox.ContextMenu>
        </ListBox>                
    </DockPanel>
</Window>

SelectAllExecuted 就是这样:

SelectAllExecuted is simply this:

private void SelectAllExecuted(object sender, ExecutedRoutedEventArgs e)
{
    listBox.SelectAll();
}

如果列表框不在焦点上,则 Control+A 有效.上下文菜单项工作正常.但如果列表框获得焦点,Control+A 将拒绝工作.

Control+A works if the listbox isn't in focus. The context menu item works correctly. But Control+A refuses to work if the listbox is focused.

我觉得我在与列表框作斗争,但我不应该这样做.

I feel like I'm fighting against the listbox, but I shouldn't need to.

编辑:看来我的全部问题都与多选模式有关.如果我将它设置为扩展,那么一切正常,但是我不希望它处于扩展模式.

Edit: It seems my entire problems are with the Multiple SelectionMode. If I set it to Extended then everything works, however I don't want it in extended mode.

推荐答案

ListBox 似乎有它自己的 Ctrl+A 组合键的内部命令,正如 Marco Zhou 解释的那样.我们还可以通过尝试在 Execute 和 Preview Execute 处理程序中放置断点来测试这一点.正如您将看到的,当 ListBox 具有焦点并且按下组合键时,两者都没有达到.即使我们将 SelectionMode 设置为 Extended 并且我们可以看到命令选择的项目,仍然无法到达处理程序.不过值得庆幸的是,我们可以通过重新分配现有的 InputGesture 来覆盖它.我们可以在 ListBox 中这样做,以摆脱它的自定义 Ctrl+A 处理,并将其重新分配给 ApplicationCommands.SelectAll 命令.

ListBox seems to have it's own internal command for the Ctrl+A key combination, as Marco Zhou explains. We can also test this by attempting to place a breakpoint in the Execute and Preview Execute handlers. As you will see neither is reached when the ListBox has focus and the key combination is pressed. Even when we set the SelectionMode to Extended and we can watch the items be selected by the command the handlers still are not reached. Thankfully though, we can override an existing InputGesture by just re-assigning it. We can do this in the ListBox to get rid of it's custom Ctrl+A handling, and re-assign it to the ApplicationCommands.SelectAll command.

<ListBox Name="listBox"
         SelectionMode="Multiple">
    <ListBox.InputBindings>
        <KeyBinding Command="ApplicationCommands.SelectAll"
                    Modifiers="Ctrl"
                    Key="A" />
    </ListBox.InputBindings>            
    ...
</ListBox>

将 KeyBinding 添加到 ListBox 后,当它获得焦点时,它现在会将 Ctrl+A 路由回您现有的 SelectAll 命令和 SelectAllExecuted.

Once the KeyBinding is added to the ListBox, when it has focus it will now route Ctrl+A back to your existing SelectAll command and SelectAllExecuted.

这篇关于WPF 列表框和全选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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