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

suu*_*suu 5 c# wpf xaml listbox

我需要在列表框项目上添加功能,用户可以通过单独单击每个项目来选择项目,也可以执行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>
Run Code Online (Sandbox Code Playgroud)

在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;
    }
}
Run Code Online (Sandbox Code Playgroud)

但Shift + Click功能无效.我是WPF的新手任何人都可以指导我.

Mar*_*een 7

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

<ListBox ItemsSource="{Binding ValuesView}" SelectionMode="Extended" />
Run Code Online (Sandbox Code Playgroud)

  • 为用户提供他们期望的用户界面,而不是您认为应该的用户界面。所有用户在选择列表中的非连续项目时都会及早学习使用Ctrl键-如果您使列表做不同的事情,他们会讨厌您的应用程序,迫使它们与其他应用程序不同。 (2认同)