/// <summary> /// Clears the current selected items and selects the given items. /// </summary> /// <param name="dropInfo">The drop information.</param> /// <param name="items">The items which should be select.</param> /// <param name="applyTemplate">if set to <c>true</c> then for all items the ApplyTemplate will be invoked.</param> /// <param name="focusVisualTarget">if set to <c>true</c> the visual target will be focused.</param> /// <exception cref="System.ArgumentNullException"><paramref name="dropInfo" /> is <see langword="null" /></exception> /// <exception cref="System.ArgumentNullException"><paramref name="dropInfo" /> is <see langword="null" /></exception> public static void SelectDroppedItems([NotNull] IDropInfo dropInfo, [NotNull] IEnumerable items, bool applyTemplate = true, bool focusVisualTarget = true) { if (dropInfo == null) { throw new ArgumentNullException(nameof(dropInfo)); } if (items == null) { throw new ArgumentNullException(nameof(items)); } if (dropInfo.VisualTarget is ItemsControl itemsControl) { var tvItem = dropInfo.VisualTargetItem as TreeViewItem; var tvItemIsExpanded = tvItem != null && tvItem.HasHeader && tvItem.HasItems && tvItem.IsExpanded; var itemsParent = tvItemIsExpanded ? tvItem : dropInfo.VisualTargetItem != null ? ItemsControl.ItemsControlFromItemContainer(dropInfo.VisualTargetItem) : itemsControl; itemsParent ??= itemsControl; (dropInfo.DragInfo.VisualSourceItem as TreeViewItem)?.ClearSelectedItems(); itemsParent.ClearSelectedItems(); var selectDroppedItems = dropInfo.VisualTarget is TabControl || (dropInfo.VisualTarget != null && DragDrop.GetSelectDroppedItems(dropInfo.VisualTarget)); if (selectDroppedItems) { foreach (var item in items) { if (applyTemplate) { // call ApplyTemplate for TabItem in TabControl to avoid this error: // // System.Windows.Data Error: 4 : Cannot find source for binding with reference var container = itemsParent.ItemContainerGenerator.ContainerFromItem(item) as FrameworkElement; container?.ApplyTemplate(); } itemsParent.SetItemSelected(item, true); } if (focusVisualTarget) { itemsControl.Focus(); } } } }
/// <summary> /// Performs a drop. /// </summary> /// <param name="dropInfo">Information about the drop.</param> public virtual void Drop(IDropInfo dropInfo) { if (dropInfo == null || dropInfo.DragInfo == null) { return; } var insertIndex = dropInfo.UnfilteredInsertIndex; var itemsControl = dropInfo.VisualTarget as ItemsControl; if (itemsControl != null) { var editableItems = itemsControl.Items as IEditableCollectionView; if (editableItems != null) { var newItemPlaceholderPosition = editableItems.NewItemPlaceholderPosition; if (newItemPlaceholderPosition == NewItemPlaceholderPosition.AtBeginning && insertIndex == 0) { ++insertIndex; } else if (newItemPlaceholderPosition == NewItemPlaceholderPosition.AtEnd && insertIndex == itemsControl.Items.Count) { --insertIndex; } } } var destinationList = dropInfo.TargetCollection.TryGetList(); var data = ExtractData(dropInfo.Data).OfType <object>().ToList(); var copyData = ShouldCopyData(dropInfo); if (!copyData) { var sourceList = dropInfo.DragInfo.SourceCollection.TryGetList(); if (sourceList != null) { foreach (var o in data) { var index = sourceList.IndexOf(o); if (index != -1) { sourceList.RemoveAt(index); // so, is the source list the destination list too ? if (destinationList != null && Equals(sourceList, destinationList) && index < insertIndex) { --insertIndex; } } } } } if (destinationList != null) { var objects2Insert = new List <object>(); // check for cloning var cloneData = dropInfo.Effects.HasFlag(DragDropEffects.Copy) || dropInfo.Effects.HasFlag(DragDropEffects.Link); foreach (var o in data) { var obj2Insert = o; if (cloneData) { var cloneable = o as ICloneable; if (cloneable != null) { obj2Insert = cloneable.Clone(); } } objects2Insert.Add(obj2Insert); destinationList.Insert(insertIndex++, obj2Insert); } var selectDroppedItems = itemsControl is TabControl || (itemsControl != null && DragDrop.GetSelectDroppedItems(itemsControl)); if (selectDroppedItems) { SelectDroppedItems(dropInfo, objects2Insert); } } }