static void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // Ignore the click if the user has clicked on a scrollbar. if (DragDrop.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 selectedItems = itemsControl.GetSelectedItems(); if (selectedItems.Cast <object>().Contains(m_DragInfo.SourceItem)) { // TODO: Re-raise the supressed event if the user didn't initiate a drag. e.Handled = true; } } }
public virtual void StartDrag(DragInfo dragInfo) { int itemCount = dragInfo.SourceItems.Cast <object>().Count(); if (itemCount == 1) { dragInfo.Data = dragInfo.SourceItems.Cast <object>().First(); } else if (itemCount > 1) { dragInfo.Data = TypeUtilities.CreateDynamicallyTypedList(dragInfo.SourceItems); } dragInfo.Effects = (dragInfo.Data != null) ? DragDropEffects.Copy | DragDropEffects.Move : DragDropEffects.None; }
public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo, string dataFormat) { this.Data = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data; this.DragInfo = dragInfo; this.VisualTarget = sender as UIElement; if (sender is ItemsControl) { ItemsControl itemsControl = (ItemsControl)sender; UIElement item = itemsControl.GetItemContainerAt(e.GetPosition(itemsControl)); this.VisualTargetOrientation = itemsControl.GetItemsPanelOrientation(); if (item != null) { ItemsControl itemParent = ItemsControl.ItemsControlFromItemContainer(item); this.InsertIndex = itemParent.ItemContainerGenerator.IndexFromContainer(item); this.TargetCollection = itemParent.ItemsSource ?? itemParent.Items; this.TargetItem = itemParent.ItemContainerGenerator.ItemFromContainer(item); this.VisualTargetItem = item; if (this.VisualTargetOrientation == Orientation.Vertical) { if (e.GetPosition(item).Y > item.RenderSize.Height / 2) { this.InsertIndex++; } } else { if (e.GetPosition(item).X > item.RenderSize.Width / 2) { this.InsertIndex++; } } } else { this.TargetCollection = itemsControl.ItemsSource ?? itemsControl.Items; this.InsertIndex = itemsControl.Items.Count; } } }