private static void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // Ignore the click if the user has clicked on a scrollbar. if (HitTestScrollBar(sender, e)) { _dragInfo = null; return; } _dragInfo = new DragInfo(sender, e); // If the sender is a list box that allows multiple selections, ensure that clicking on an // already selected item does not change the selection, otherwise dragging multiple items // is made impossible. var itemsControl = sender as ItemsControl; if (_dragInfo.VisualSourceItem == null || itemsControl == null || !itemsControl.CanSelectMultipleItems()) { return; } var selectedItems = itemsControl .GetSelectedItems() .Cast <object>() .ToList(); if (selectedItems.Count() <= 1 || !selectedItems.Contains(_dragInfo.SourceItem)) { return; } _clickSupressItem = _dragInfo.SourceItem; e.Handled = true; }
/// <summary> /// Initializes a new instance of the DropInfo class. /// </summary> /// <param name="sender"> /// The sender of the drag event. /// </param> /// <param name="e"> /// The drag event. /// </param> /// <param name="dragInfo"> /// Information about the source of the drag, if the drag came from within the framework. /// </param> public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo) { string dataFormat = DragDrop.DataFormat.Name; Data = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data; DragInfo = dragInfo; VisualTarget = sender as UIElement; if (VisualTarget == null) return; DropPosition = e.GetPosition(VisualTarget); if (!(sender is ItemsControl)) return; var itemsControl = (ItemsControl)sender; UIElement item = itemsControl.GetItemContainerAt(DropPosition); bool directlyOverItem = item != null; TargetGroup = FindGroup(itemsControl, DropPosition); VisualTargetOrientation = itemsControl.GetItemsPanelOrientation(); if (item == null) { item = itemsControl.GetItemContainerAt(DropPosition, VisualTargetOrientation); directlyOverItem = false; } if (item == null) { TargetCollection = itemsControl.ItemsSource ?? itemsControl.Items; InsertIndex = itemsControl.Items.Count; return; } ItemsControl itemParent = ItemsControl.ItemsControlFromItemContainer(item); InsertIndex = itemParent.ItemContainerGenerator.IndexFromContainer(item); TargetCollection = itemParent.ItemsSource ?? itemParent.Items; if (directlyOverItem) { TargetItem = itemParent.ItemContainerGenerator.ItemFromContainer(item); VisualTargetItem = item; } if (VisualTargetOrientation == Orientation.Vertical) { if (e.GetPosition(item).Y > item.RenderSize.Height / 2) InsertIndex++; } else { if (e.GetPosition(item).X > item.RenderSize.Width / 2) InsertIndex++; } }
private static void DragSource_PreviewMouseMove(object sender, MouseEventArgs e) { if (_dragInfo == null || _dragInProgress) { return; } Point dragStart = _dragInfo.DragStartPosition; Point position = e.GetPosition(null); if (Math.Abs(position.X - dragStart.X) <= SystemParameters.MinimumHorizontalDragDistance && Math.Abs(position.Y - dragStart.Y) <= SystemParameters.MinimumVerticalDragDistance) { return; } IDragSource dragHandler = GetDragHandler(_dragInfo.VisualSource); if (dragHandler != null) { dragHandler.StartDrag(_dragInfo); } else { DefaultDragHandler.StartDrag(_dragInfo); } if (_dragInfo.Effects == DragDropEffects.None || _dragInfo.Data == null) { return; } var data = new DataObject(DataFormat.Name, _dragInfo.Data); try { _dragInProgress = true; System.Windows.DragDrop.DoDragDrop(_dragInfo.VisualSource, data, _dragInfo.Effects); } finally { _dragInProgress = false; } _dragInfo = null; }
private static void DragSource_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { // If we prevented the control's default selection handling in DragSource_PreviewMouseLeftButtonDown // by setting 'e.Handled = true' and a drag was not initiated, manually set the selection here. var itemsControl = sender as ItemsControl; if (itemsControl != null && _dragInfo != null && _clickSupressItem == _dragInfo.SourceItem) { if ((Keyboard.Modifiers & ModifierKeys.Control) != 0) { itemsControl.SetItemSelected(_dragInfo.SourceItem, false); } else { itemsControl.SetSelectedItem(_dragInfo.SourceItem); } } _dragInfo = null; _clickSupressItem = null; }
/// <summary> /// Initializes a new instance of the DropInfo class. /// </summary> /// <param name="sender"> /// The sender of the drag event. /// </param> /// <param name="e"> /// The drag event. /// </param> /// <param name="dragInfo"> /// Information about the source of the drag, if the drag came from within the framework. /// </param> public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo) { string dataFormat = DragDrop.DataFormat.Name; Data = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data; DragInfo = dragInfo; VisualTarget = sender as UIElement; if (VisualTarget == null) { return; } DropPosition = e.GetPosition(VisualTarget); if (!(sender is ItemsControl)) { return; } var itemsControl = (ItemsControl)sender; UIElement item = itemsControl.GetItemContainerAt(DropPosition); bool directlyOverItem = item != null; TargetGroup = FindGroup(itemsControl, DropPosition); VisualTargetOrientation = itemsControl.GetItemsPanelOrientation(); if (item == null) { item = itemsControl.GetItemContainerAt(DropPosition, VisualTargetOrientation); directlyOverItem = false; } if (item == null) { TargetCollection = itemsControl.ItemsSource ?? itemsControl.Items; InsertIndex = itemsControl.Items.Count; return; } ItemsControl itemParent = ItemsControl.ItemsControlFromItemContainer(item); InsertIndex = itemParent.ItemContainerGenerator.IndexFromContainer(item); TargetCollection = itemParent.ItemsSource ?? itemParent.Items; if (directlyOverItem) { TargetItem = itemParent.ItemContainerGenerator.ItemFromContainer(item); VisualTargetItem = item; } if (VisualTargetOrientation == Orientation.Vertical) { if (e.GetPosition(item).Y > item.RenderSize.Height / 2) { InsertIndex++; } } else { if (e.GetPosition(item).X > item.RenderSize.Width / 2) { InsertIndex++; } } }
private static void DragSource_PreviewMouseMove(object sender, MouseEventArgs e) { if (_dragInfo == null || _dragInProgress) return; Point dragStart = _dragInfo.DragStartPosition; Point position = e.GetPosition(null); if (Math.Abs(position.X - dragStart.X) <= SystemParameters.MinimumHorizontalDragDistance && Math.Abs(position.Y - dragStart.Y) <= SystemParameters.MinimumVerticalDragDistance) return; IDragSource dragHandler = GetDragHandler(_dragInfo.VisualSource); if (dragHandler != null) dragHandler.StartDrag(_dragInfo); else DefaultDragHandler.StartDrag(_dragInfo); if (_dragInfo.Effects == DragDropEffects.None || _dragInfo.Data == null) return; var data = new DataObject(DataFormat.Name, _dragInfo.Data); try { _dragInProgress = true; System.Windows.DragDrop.DoDragDrop(_dragInfo.VisualSource, data, _dragInfo.Effects); } finally { _dragInProgress = false; } _dragInfo = null; }
private static void DragSource_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { // If we prevented the control's default selection handling in DragSource_PreviewMouseLeftButtonDown // by setting 'e.Handled = true' and a drag was not initiated, manually set the selection here. var itemsControl = sender as ItemsControl; if (itemsControl != null && _dragInfo != null && _clickSupressItem == _dragInfo.SourceItem) { if ((Keyboard.Modifiers & ModifierKeys.Control) != 0) itemsControl.SetItemSelected(_dragInfo.SourceItem, false); else itemsControl.SetSelectedItem(_dragInfo.SourceItem); } _dragInfo = null; _clickSupressItem = null; }
private static void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // Ignore the click if the user has clicked on a scrollbar. if (HitTestScrollBar(sender, e)) { _dragInfo = null; return; } _dragInfo = new DragInfo(sender, e); // If the sender is a list box that allows multiple selections, ensure that clicking on an // already selected item does not change the selection, otherwise dragging multiple items // is made impossible. var itemsControl = sender as ItemsControl; if (_dragInfo.VisualSourceItem == null || itemsControl == null || !itemsControl.CanSelectMultipleItems()) return; var selectedItems = itemsControl .GetSelectedItems() .Cast<object>() .ToList(); if (selectedItems.Count() <= 1 || !selectedItems.Contains(_dragInfo.SourceItem)) return; _clickSupressItem = _dragInfo.SourceItem; e.Handled = true; }