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. ItemsControl itemsControl = sender as ItemsControl; 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); } } if (m_DragInfo != null) { m_DragInfo = null; } m_ClickSupressItem = null; }
static void DragSource_PreviewMouseMove(object sender, MouseEventArgs e) { if (m_DragInfo != null && !m_DragInProgress) { Point dragStart = m_DragInfo.DragStartPosition; Point position = e.GetPosition(null); if (Math.Abs(position.X - dragStart.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(position.Y - dragStart.Y) > SystemParameters.MinimumVerticalDragDistance) { IDragSource dragHandler = GetDragHandler(m_DragInfo.VisualSource); if (dragHandler != null) { dragHandler.StartDrag(m_DragInfo); } else { DefaultDragHandler.StartDrag(m_DragInfo); } if (m_DragInfo.Effects != DragDropEffects.None && m_DragInfo.Data != null) { DataObject data = new DataObject(DataFormat.Name, m_DragInfo.Data); try { m_DragInProgress = true; System.Windows.DragDrop.DoDragDrop(m_DragInfo.VisualSource, data, m_DragInfo.Effects); } finally { m_DragInProgress = false; } m_DragInfo = null; } } } }
static void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // Ignore the click if the user has clicked on a scrollbar. if (HitTestScrollBar(sender, e)) { m_DragInfo = null; return; } m_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. ItemsControl itemsControl = sender as ItemsControl; if (m_DragInfo.VisualSourceItem != null && itemsControl != null && itemsControl.CanSelectMultipleItems()) { IEnumerable<object> selectedItems = itemsControl.GetSelectedItems().Cast<object>(); if (selectedItems.Count() > 1 && selectedItems.Contains(m_DragInfo.SourceItem)) { m_ClickSupressItem = m_DragInfo.SourceItem; e.Handled = true; } } }