使用WPF移位+单击列表框项目上的功能 [英] shift +click functionality on listbox item using WPF

查看:69
本文介绍了使用WPF移位+单击列表框项目上的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在列表框项目"上添加功能,用户可以通过单击单个项目来选择该项目,也可以按住Shift键并单击以在列表中选择一系列项目.

I need to add functionality on List box Item where the user can select the item by clicking each item individually and can also do shift+click to select a series of items in the list.

<ListBox ItemsSource="{Binding ItemFields, Mode=TwoWay}"
         VerticalAlignment="Stretch" HorizontalAlignment="Left"
         Margin="16,156,0,34" Name="fRListbox" Width="499" >                   
    <ListBox.ItemContainerStyle>                            
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="reportDatagrid_MouseDown"/>                              
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

在xaml.cs上,我编写了以下代码:

and on xaml.cs I wrote below code:

private void ListItem_MouseClick(object sender, MouseButtonEventArgs e)
{
    if ((e.LeftButton == MouseButtonState.Pressed) && Keyboard.IsKeyDown(Key.RightShift))
    {
        fRListbox.SelectionMode = SelectionMode.Extended;
    }
    else if ((e.LeftButton == MouseButtonState.Pressed) && Keyboard.IsKeyDown(Key.LeftShift))
    {
        fRListbox.SelectionMode = SelectionMode.Multiple;
    }
    else if (e.LeftButton == MouseButtonState.Pressed)
    {
        fRListbox.SelectionMode = SelectionMode.Multiple;
    }
}

但是Shift + Click功能不起作用. 我是WPF的新手,任何人都可以指导我.

But Shift +Click functionality is not working. I am new to WPF can anyone guide me.

推荐答案

如果您希望用户在单击单个项目(例如Windows资源管理器以及几乎所有其他类型的项目)时按住Ctrl键来选择项目,列表),然后将SelectionMode设置为Extended是使用CtrlShift键进行单项和多项选择的最简单方法.

If you're happy for users to select items by holding the Ctrl key down when clicking individual items (like Windows Explorer and pretty much every other type of list) then setting SelectionMode to Extended is the simplest way to achieve single and multiple selections using the Ctrl and Shift keys.

<ListBox ItemsSource="{Binding ValuesView}" SelectionMode="Extended" />

这篇关于使用WPF移位+单击列表框项目上的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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