Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the DragInfo class.
        /// </summary>
        ///
        /// <param name="sender">
        /// The sender of the mouse event that initiated the drag.
        /// </param>
        ///
        /// <param name="e">
        /// The mouse event that initiated the drag.
        /// </param>
        public DragInfo(object sender, MouseButtonEventArgs e)
        {
            Effects              = DragDropEffects.None;
            MouseButton          = e.ChangedButton;
            VisualSource         = sender as UIElement;
            DragStartPosition    = e.GetPosition(VisualSource);
            DragDropCopyKeyState = DragDrop.GetDragDropCopyKeyState(VisualSource);

            var dataFormat = DragDrop.GetDataFormat(VisualSource);

            if (dataFormat != null)
            {
                DataFormat = dataFormat;
            }

            var sourceElement = e.OriginalSource as UIElement;

            // If we can't cast object as a UIElement it might be a FrameworkContentElement, if so try and use its parent.
            if (sourceElement == null && e.OriginalSource is FrameworkContentElement element)
            {
                sourceElement = element.Parent as UIElement;
            }

            if (sender is ItemsControl itemsControl)
            {
                SourceGroup = itemsControl.FindGroup(DragStartPosition);
                VisualSourceFlowDirection = itemsControl.GetItemsPanelFlowDirection();

                UIElement item = null;
                if (sourceElement != null)
                {
                    item = itemsControl.GetItemContainer(sourceElement);
                }

                if (item == null)
                {
                    item = DragDrop.GetDragDirectlySelectedOnly(VisualSource) ? itemsControl.GetItemContainerAt(e.GetPosition(itemsControl)) : itemsControl.GetItemContainerAt(e.GetPosition(itemsControl), itemsControl.GetItemsPanelOrientation());
                }

                if (item != null)
                {
                    // Remember the relative position of the item being dragged
                    PositionInDraggedItem = e.GetPosition(item);

                    var itemParent = ItemsControl.ItemsControlFromItemContainer(item);

                    if (itemParent != null)
                    {
                        SourceCollection = itemParent.ItemsSource ?? itemParent.Items;
                        if (itemParent != itemsControl)
                        {
                            if (item is TreeViewItem tvItem)
                            {
                                var tv = tvItem.GetVisualAncestor <TreeView>();
                                if (tv != null && !Equals(tv, itemsControl) && !tv.IsDragSource())
                                {
                                    return;
                                }
                            }
                            else if (itemsControl.ItemContainerGenerator.IndexFromContainer(itemParent) < 0 && !itemParent.IsDragSource())
                            {
                                return;
                            }
                        }
                        SourceIndex = itemParent.ItemContainerGenerator.IndexFromContainer(item);
                        SourceItem  = itemParent.ItemContainerGenerator.ItemFromContainer(item);
                    }
                    else
                    {
                        SourceIndex = -1;
                    }

                    var selectedItems = itemsControl.GetSelectedItems().OfType <object>().Where(i => i != CollectionView.NewItemPlaceholder).ToList();
                    SourceItems = selectedItems;

                    // Some controls (I'm looking at you TreeView!) haven't updated their
                    // SelectedItem by this point. Check to see if there 1 or less item in
                    // the SourceItems collection, and if so, override the control's SelectedItems with the clicked item.
                    //
                    // The control has still the old selected items at the mouse down event, so we should check this and give only the real selected item to the user.
                    if (selectedItems.Count <= 1 || SourceItem != null && !selectedItems.Contains(SourceItem))
                    {
                        SourceItems = Enumerable.Repeat(SourceItem, 1);
                    }

                    VisualSourceItem = item;
                }
                else
                {
                    SourceCollection = itemsControl.ItemsSource ?? itemsControl.Items;
                }
            }
            else
            {
                SourceItem = (sender as FrameworkElement)?.DataContext;
                if (SourceItem != null)
                {
                    SourceItems = Enumerable.Repeat(SourceItem, 1);
                }
                VisualSourceItem      = sourceElement;
                PositionInDraggedItem = sourceElement != null?e.GetPosition(sourceElement) : DragStartPosition;
            }

            if (SourceItems == null)
            {
                SourceItems = Enumerable.Empty <object>();
            }
        }