Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shift + Click item of multi-selectable Listbox doesn't work as windows file explorer #127

Closed
julia-chen opened this issue Mar 11, 2015 · 0 comments
Labels
Milestone

Comments

@julia-chen
Copy link

This issue will occur when DragSource is Listbox which SelectionMode is Extend.

For example, there are 100 items in Listbox.

  1. Click at the 10th item.
  2. Press shift-key and select the 5th item.
  3. Press shift-key and select the 7th item.

[Expect Result]
7th item ~ 10th item will be selected

[Actual Result]
only 7th item is selected.

After step 3, if shift + click any item, the result is not the same as in windows file explorer.

DragDrop.DragSource_PreviewMouseLeftButtonUp
{
...
      if (itemsControl != null && m_DragInfo != null && m_ClickSupressItem == m_DragInfo.SourceItem) {
        if ((Keyboard.Modifiers & ModifierKeys.Control) != 0) {
          itemsControl.SetItemSelected(m_DragInfo.SourceItem, false);
        } else {
          itemsControl.SetSelectedItem(m_DragInfo.SourceItem);
        }
      }
...
}

ItemsControlExtensions.SetSelectedItem
{
...
      } else if (itemsControl is ListBox) {
        ((ListBox)itemsControl).SelectedItem = null;
        ((ListBox)itemsControl).SelectedItem = item;
      }
...
}

I modify the code to

DragDrop.DragSource_PreviewMouseLeftButtonUp
{
...
        if ((Keyboard.Modifiers & ModifierKeys.Control) != 0) {
          itemsControl.SetItemSelected(m_DragInfo.SourceItem, false);
        }
        else if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0)
        {
            object selecteditem = null;
            foreach (var item in itemsControl.GetSelectedItems())
            {
                selecteditem = item;
                break;
            }

            if (selecteditem != null)
            {
                itemsControl.SetSelectedItem(selecteditem);

                if (selecteditem != m_DragInfo.SourceItem)
                {
                    bool selectflag = false;
                    object startitem = null, enditem = null;

                    foreach (var item in itemsControl.Items)
                    {
                        if (item == m_DragInfo.SourceItem ||
                            item == selecteditem)
                        {
                            selectflag = true;
                            if (null == startitem)
                                startitem = item;
                            else
                                enditem = item;
                        }

                        if (itemsControl.GetItemSelected(item) != selectflag)
                            itemsControl.SetItemSelected(item, selectflag);

                        if (true == selectflag)
                        {
                            if (enditem == item)
                                selectflag = false;
                        }
                    }
                }
            }
        }
        else
        {
            itemsControl.SetSelectedItem(m_DragInfo.SourceItem);
        }
...
}

ItemsControlExtensions.SetSelectedItem
{
...
      } else if (itemsControl is ListBox) {

          SelectionMode mode = ((ListBox)itemsControl).SelectionMode;

          try
          {   // change SelectionMode for UpdateAnchorAndActionItem
              ((ListBox)itemsControl).SelectionMode = SelectionMode.Single;
              ((ListBox)itemsControl).SelectedItem = null;
              ((ListBox)itemsControl).SelectedItem = item;
          }
          finally
          {
              ((ListBox)itemsControl).SelectionMode = mode;
          }

      }...
}
@punker76 punker76 added the Bug label Apr 11, 2015
@punker76 punker76 added this to the v1.0.0 milestone Apr 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants