/// <summary> /// Constructor. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <param name="dragInfo"></param> /// <param name="dataFormat"></param> public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo, string dataFormat) { Data = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data; DragInfo = dragInfo; VisualTarget = sender as UIElement; if (sender is ItemsControl) { ItemsControl itemsControl = (ItemsControl)sender; Point p = e.GetPosition(itemsControl); UIElement item = itemsControl.GetItemContainerAt(p); if (item == null) { // try again with margins p.X -= 15; p.Y -= 15; item = itemsControl.GetItemContainerAt(p); } VisualTargetOrientation = itemsControl.GetItemsPanelOrientation(); if (item != null) { ItemsControl itemParent = ItemsControl.ItemsControlFromItemContainer(item); InsertIndex = itemParent.ItemContainerGenerator.IndexFromContainer(item); TargetCollection = itemParent.ItemsSource ?? itemParent.Items; 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++; } } } else { TargetCollection = itemsControl.ItemsSource ?? itemsControl.Items; InsertIndex = itemsControl.Items.Count; } } this.IsNotHandled = false; }
/// <summary> /// Constructor. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <param name="dragInfo"></param> /// <param name="dataFormat"></param> public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo, string dataFormat) { Data = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data; DragInfo = dragInfo; VisualTarget = sender as UIElement; if (sender is ItemsControl) { ItemsControl itemsControl = (ItemsControl)sender; Point p = e.GetPosition(itemsControl); UIElement item = itemsControl.GetItemContainerAt(p); if (item == null) { // try again with margins p.X -= 15; p.Y -= 15; item = itemsControl.GetItemContainerAt(p); } VisualTargetOrientation = itemsControl.GetItemsPanelOrientation(); if (item != null) { ItemsControl itemParent = ItemsControl.ItemsControlFromItemContainer(item); InsertIndex = itemParent.ItemContainerGenerator.IndexFromContainer(item); TargetCollection = itemParent.ItemsSource ?? itemParent.Items; 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++; } } else { TargetCollection = itemsControl.ItemsSource ?? itemsControl.Items; InsertIndex = itemsControl.Items.Count; } } this.IsNotHandled = false; }
/// <summary> /// Starts drag drop. /// </summary> /// <param name="dragInfo">Info.</param> 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; }
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; // ignore the event if we reraised it to assure correct behavior if (ignoreEventOnce) { if (itemsControl is Selector && sourceItemTemp != null) { ((Selector)itemsControl).SelectedItem = null; } ignoreEventOnce = false; return; } if (m_DragInfo.VisualSourceItem != null && itemsControl != null && itemsControl.CanSelectMultipleItems()) { IEnumerable selectedItems = itemsControl.GetSelectedItems(); if (selectedItems.Cast <object>().Contains(m_DragInfo.SourceItem)) { if (e.ClickCount == 1) { if (itemsControl is Selector && Keyboard.Modifiers != ModifierKeys.Shift && Keyboard.Modifiers != ModifierKeys.Control) { sourceItemTemp = m_DragInfo.SourceItem; argsSaved = e; e.Handled = true; } } } } }
/// <summary> /// Starts drag drop. /// </summary> /// <param name="dragInfo">Info.</param> 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; }
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; // ignore the event if we reraised it to assure correct behavior if (ignoreEventOnce) { if (itemsControl is Selector && sourceItemTemp != null) ((Selector)itemsControl).SelectedItem = null; ignoreEventOnce = false; return; } if (m_DragInfo.VisualSourceItem != null && itemsControl != null && itemsControl.CanSelectMultipleItems()) { IEnumerable selectedItems = itemsControl.GetSelectedItems(); if (selectedItems.Cast<object>().Contains(m_DragInfo.SourceItem)) { if( e.ClickCount == 1 ) if (itemsControl is Selector && Keyboard.Modifiers != ModifierKeys.Shift && Keyboard.Modifiers != ModifierKeys.Control) { sourceItemTemp = m_DragInfo.SourceItem; argsSaved = e; e.Handled = true; } } } }