Skip to content

ListBox with Extended SelectionMode "remembers" start of previous selection range separated by single item selection #406

Closed

Description

@clemarescx

To Reproduce
Steps to reproduce the behavior in the Showcase project:

  1. Go to ListBox > Bound (the default view when starting the project)
  2. In the LeftBoundListBox, select item 2
  3. Hold shift, and select item 10
    • Range 2 to 10 is selected, as expected
  4. Select a single item within the previous range by clicking without holding shift, e.g item 5
    • Only item 5 is selected, as expected
  5. Hold shift and click an item with index higher than previously selected item, e.g item 7
    • Range 2 to 7 is selected, which wasn't expected

Note: this does not occur when setting dd:DragDrop.DragSourceIgnore="True" on the ListBox.

Expected behavior
Only the items in the selected range should be selected.

Findings
A similar issue occurs "backwards":

  1. select item 10 first
  2. hold shift and select item 2
  3. select a single item 7
  4. hold shift and select item 5
    • range 10 to 5 will be selected

SelectionChanged event is raised multiple times when going from selected range to single selection

  • When selecting the single item between the two range selections, the SelectionChanged event on the ListBox is raised multiple times, each time removing a single item from SelectedItems, until only the single item is selected.
  • To view this, add the following handler to LeftBoundListBox:
    ListBoxSamples.xaml:
     <ListBox x:Name="LeftBoundListBox"
               ...
               SelectionChanged="LeftBoundListBox_OnSelectionChanged" />
    ListBoxSamples.xaml.cs:
    private void LeftBoundListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var selectedItems = this.LeftBoundListBox.SelectedItems.OfType<ItemModel>().ToList();
        var itemIndices = selectedItems.Select(svm => $"{svm.Index}");
        var indexList = string.Join(", ", itemIndices);
        Debug.WriteLine($"selected: : {indexList}");
    }
    When running the same sequence as described in To reproduce, the expected output will be:
    selected: : 2
    selected: : 2, 3, 4, 5, 6, 7, 8, 9, 10
    selected: : 3, 4, 5, 6, 7, 8, 9, 10
    selected: : 4, 5, 6, 7, 8, 9, 10
    selected: : 5, 6, 7, 8, 9, 10
    selected: : 5, 7, 8, 9, 10
    selected: : 5, 8, 9, 10
    selected: : 5, 9, 10
    selected: : 5, 10
    selected: : 5
    selected: : 5, 2, 3, 4, 6, 7
    

Demo of the sequence
pgkOKXaHWS

Desktop (please complete the following information):

  • OS: Microsoft Windows 10 Pro
  • Version 10.0.19043 Build 19043

Additional context
Tested with VS 2019 and VS 2022 Preview 4.1:

  • .NET Framework 4.5
  • .NET Framework 4.6.2

Activity

  1. added this to the 3.0.0 milestone on Oct 8, 2021
  2. added a commit that references this issue on Oct 8, 2021
  3. modified the milestones: 3.0.0, 2.4.3 on Oct 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions