shift +click functionality on listbox item using WPF

我是研究僧i 提交于 2019-12-10 15:44:53

问题


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>

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

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


回答1:


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


来源:https://stackoverflow.com/questions/22354509/shift-click-functionality-on-listbox-item-using-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!