【问题标题】:shift +click functionality on listbox item using WPF使用 WPF 对列表框项目进行 shift +单击功能
【发布时间】:2014-03-12 14:23:15
【问题描述】:

我需要在列表框项目上添加功能,用户可以通过单独单击每个项目来选择项目,也可以通过 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 + 单击功能不起作用。 我是 WPF 新手,谁能指导我。

【问题讨论】:

    标签: c# wpf xaml listbox


    【解决方案1】:

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

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

    【讨论】:

    • 是的,我已经尝试过了,但这并不能通过单独单击每个项目来选择项目。我需要两种选择模式。请帮助。
    • 为您的用户提供他们期望的 UI,而不是您认为他们应该拥有的 UI。在选择列表中不连续的项目时,所有用户都会很早就学会使用 Ctrl 键 - 如果您让列表做一些不同的事情,他们会讨厌您的应用程序强迫他们以不同于其他应用程序的方式工作。
    • 我需要这两个功能,因为以前的选择模式是“多重”,它正在对单个项目进行点击选择,但用户端的要求是也有 shift+click,这样可以减轻项目的系列选择。
    • @MarkGreen 允许在不按住 Ctrl 的情况下进行多选是完全合理的,具体取决于用例。毕竟,WPF 确实为此目的提供了SelectionMode.Multiple。这里的问题是SelectionMode.Multiple 禁用了使用 Shift+Click 选择连续项目或 Ctrl+A 选择全部的功能,这似乎是一个错误/疏忽。
    猜你喜欢
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2012-11-26
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多