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

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

问题描述

我想创建一个简单的ListBox,并将SelectAll作为上下文菜单项。但是看来ListBox有一些内置的处理SelectAll,我不能工作,但是干扰我尝试实现SelectAll。



我的整个XAML是这:

 < Window x:Class =WpfApplication1.Window1
xmlns =http:// schemas .microsoft.com / winfx / 2006 / xaml / presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Title =Window1Height = 300Width =300>
< Window.CommandBindings>
< CommandBinding Command =ApplicationCommands.SelectAll
Executed =SelectAllExecuted/>
< /Window.CommandBindings>
< DockPanel>
< CheckBox DockPanel.Dock =Top>我的复选框< / CheckBox>
< ListBox Name =listBoxSelectionMode =Multiple>
< ListBox.ContextMenu>
< ContextMenu>
< MenuItem Command =ApplicationCommands.SelectAll/>
< / ContextMenu>
< /ListBox.ContextMenu>
< / ListBox>
< / DockPanel>
< / Window>

SelectAllExecuted就是这样:

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

Control + A如果列表框未对焦。上下文菜单项正常工作。但是如果列表框是集中的话,Control + A拒绝工作。



我觉得我打击列表框,但我不需要。 >

编辑:看来我的整个问题都是使用多选择模式。如果我将它设置为扩展,那么一切工作,但是我不想在扩展模式。

解决方案

ListBox似乎有它自己的Ctrl + A组合键的内部命令,正如Marco Zhou解释。我们也可以通过尝试在Execute和Preview Execute处理程序中放置断点来测试这个。正如你将看到当ListBox有焦点和按下组合键时没有达到。即使我们将SelectionMode设置为Extended,我们可以看到命令选择的项目仍然未到达处理程序。幸运的是,我们可以通过重新分配一个现有的InputGesture。我们可以在ListBox中这样做,以摆脱它的自定义Ctrl + A处理,并重新分配给ApplicationCommands.SelectAll命令。

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

一旦将KeyBinding添加到ListBox中,当它有焦点时,它会将Ctrl + A到您现有的SelectAll命令和SelectAllExecuted。


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.

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 is simply this:

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

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

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天全站免登陆