diff --git a/GongSolutions.Wpf.DragDrop/DragDrop.cs b/GongSolutions.Wpf.DragDrop/DragDrop.cs index 49876809..0fd15823 100644 --- a/GongSolutions.Wpf.DragDrop/DragDrop.cs +++ b/GongSolutions.Wpf.DragDrop/DragDrop.cs @@ -716,9 +716,8 @@ private static void DragSource_PreviewMouseLeftButtonDown(object sender, MouseBu var itemsControl = sender as ItemsControl; if (m_DragInfo.VisualSourceItem != null && itemsControl != null && itemsControl.CanSelectMultipleItems()) { - var selectedItems = itemsControl.GetSelectedItems().Cast(); - - if (selectedItems.Count() > 1 && selectedItems.Contains(m_DragInfo.SourceItem)) { + var selectedItems = itemsControl.GetSelectedItems().OfType().ToList(); + if (selectedItems.Count() > 1 && selectedItems.Contains(m_DragInfo.SourceItem) && (Keyboard.Modifiers & ModifierKeys.Shift) == 0) { m_ClickSupressItem = m_DragInfo.SourceItem; e.Handled = true; } @@ -734,7 +733,7 @@ private static void DragSource_PreviewMouseLeftButtonUp(object sender, MouseButt if (itemsControl != null && m_DragInfo != null && m_ClickSupressItem == m_DragInfo.SourceItem) { if ((Keyboard.Modifiers & ModifierKeys.Control) != 0) { itemsControl.SetItemSelected(m_DragInfo.SourceItem, false); - } else { + } else if ((Keyboard.Modifiers & ModifierKeys.Shift) == 0) { itemsControl.SetSelectedItem(m_DragInfo.SourceItem); } } diff --git a/GongSolutions.Wpf.DragDrop/Utilities/ItemsControlExtensions.cs b/GongSolutions.Wpf.DragDrop/Utilities/ItemsControlExtensions.cs index 4324510e..c5ccc66a 100644 --- a/GongSolutions.Wpf.DragDrop/Utilities/ItemsControlExtensions.cs +++ b/GongSolutions.Wpf.DragDrop/Utilities/ItemsControlExtensions.cs @@ -230,8 +230,15 @@ public static void SetSelectedItem(this ItemsControl itemsControl, object item) ((MultiSelector)itemsControl).SelectedItem = null; ((MultiSelector)itemsControl).SelectedItem = item; } else if (itemsControl is ListBox) { - ((ListBox)itemsControl).SelectedItem = null; - ((ListBox)itemsControl).SelectedItem = item; + var selectionMode = ((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 = selectionMode; + } } else if (itemsControl is TreeView) { ((TreeView)itemsControl).SetValue(TreeView.SelectedItemProperty, null); ((TreeView)itemsControl).SetValue(TreeView.SelectedItemProperty, item); @@ -241,6 +248,20 @@ public static void SetSelectedItem(this ItemsControl itemsControl, object item) } } + public static object GetSelectedItem(this ItemsControl itemsControl) + { + if (itemsControl is MultiSelector) { + return ((MultiSelector)itemsControl).SelectedItem; + } else if (itemsControl is ListBox) { + return ((ListBox)itemsControl).SelectedItem; + } else if (itemsControl is TreeView) { + return ((TreeView)itemsControl).GetValue(TreeView.SelectedItemProperty); + } else if (itemsControl is Selector) { + return ((Selector)itemsControl).SelectedItem; + } + return null; + } + public static IEnumerable GetSelectedItems(this ItemsControl itemsControl) { //if (itemsControl.GetType().IsAssignableFrom(typeof(MultiSelector)))