繁体   English   中英

使用WPF在列表框项上移动+单击功能

[英]shift +click functionality on listbox item using WPF

我需要在列表框项目上添加功能,用户可以通过单独单击每个项目来选择项目,也可以执行shift +单击以选择列表中的一系列项目。

<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上我写了下面的代码:

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的新手任何人都可以指导我。

如果您对用户在单击单个项目时Ctrl键(如Windows资源管理器和几乎所有其他类型的列表)选择项目感到高兴,那么将SelectionMode设置为Extended是使用该选项实现单个和多个选择的最简单方法。 CtrlShift键。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM