Esempio n. 1
0
        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 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;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Create hint adorners for all visible and potential drop targets upon drag start.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="dragInfo"></param>
        private static void CreateHintAdorners(object sender, DragInfo dragInfo)
        {
            UIElement excludeElement = null;

            if (sender is UIElement exclude)
            {
                excludeElement = GetIsDropTarget(exclude) ? exclude : exclude.TryGetNextAncestorDropTargetElement();
            }

            foreach (var potentialTarget in _hintDropZones.ToList())
            {
                if (!potentialTarget.TryGetTarget(out var target))
                {
                    // Remove dead references
                    _hintDropZones.Remove(potentialTarget);
                }
                if (excludeElement != null && ReferenceEquals(excludeElement, target))
                {
                    // Dp not highlight element from sender
                    continue;
                }

                AddHintAdorner(dragInfo, target);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the DropInfo class.
        /// </summary>
        ///
        /// <param name="sender">
        /// The sender of the drag event.
        /// </param>
        ///
        /// <param name="e">
        /// The drag event.
        /// </param>
        ///
        /// <param name="dragInfo">
        /// Information about the source of the drag, if the drag came from within the framework.
        /// </param>
        public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
        {
            string dataFormat = DragDrop.DataFormat.Name;

            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        position         = e.GetPosition(itemsControl);
                UIElement    item             = itemsControl.GetItemContainerAt(position);
                bool         directlyOverItem = item != null;

                TargetGroup             = FindGroup(itemsControl, position);
                VisualTargetOrientation = itemsControl.GetItemsPanelOrientation();

                if (item == null)
                {
                    item             = itemsControl.GetItemContainerAt(position, VisualTargetOrientation);
                    directlyOverItem = false;
                }

                if (item != null)
                {
                    ItemsControl itemParent = ItemsControl.ItemsControlFromItemContainer(item);

                    InsertIndex      = itemParent.ItemContainerGenerator.IndexFromContainer(item);
                    TargetCollection = itemParent.ItemsSource ?? itemParent.Items;

                    if (directlyOverItem)
                    {
                        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;
                }
            }
        }
Esempio n. 4
0
        private static void AddHintAdorner(DragInfo dragInfo, UIElement target)
        {
            DropTargetHintAdorner adorner = null;

            if (target == null)
            {
                return;
            }

            var hintDropHandler = TryGetHintDropHandler(null, target);

            if (hintDropHandler == null)
            {
                return;
            }

            var hintAdornerType = hintDropHandler.GetHintAdorner(dragInfo);

            if (hintAdornerType == null)
            {
                return;
            }
            var adornedElement = GetAdornedElement(target);

            if (!hintAdornerType.IsInstanceOfType(typeof(DropTargetHintAdorner)) && adornedElement.IsVisible)
            {
                adorner = DropTargetHintAdorner.Create(hintAdornerType, adornedElement);
            }
            if (adorner != null)
            {
                _hintDropTargetAdorners.Add(adorner);
            }
        }
Esempio n. 5
0
        private static void DoMouseButtonDown(object sender, MouseButtonEventArgs e)
        {
            m_DragInfo = null;

            // Ignore the click if clickCount != 1 or the user has clicked on a scrollbar.
            var elementPosition = e.GetPosition((IInputElement)sender);

            if (e.ClickCount != 1 ||
                (sender as UIElement).IsDragSourceIgnored() ||
                (e.Source as UIElement).IsDragSourceIgnored() ||
                (e.OriginalSource as UIElement).IsDragSourceIgnored() ||
                (sender is TabControl) && !HitTestUtilities.HitTest4Type <TabPanel>(sender, elementPosition) ||
                HitTestUtilities.HitTest4Type <RangeBase>(sender, elementPosition) ||
                HitTestUtilities.HitTest4Type <TextBoxBase>(sender, elementPosition) ||
                HitTestUtilities.HitTest4Type <PasswordBox>(sender, elementPosition) ||
                HitTestUtilities.HitTest4Type <ComboBox>(sender, elementPosition) ||
                HitTestUtilities.HitTest4GridViewColumnHeader(sender, elementPosition) ||
                HitTestUtilities.HitTest4DataGridTypes(sender, elementPosition) ||
                HitTestUtilities.IsNotPartOfSender(sender, e))
            {
                return;
            }

            var dragInfo = new DragInfo(sender, e);

            if (dragInfo.VisualSource is ItemsControl control && control.CanSelectMultipleItems())
            {
                control.Focus();
            }

            if (dragInfo.VisualSourceItem == null)
            {
                return;
            }

            var dragHandler = TryGetDragHandler(dragInfo, sender as UIElement);

            if (!dragHandler.CanStartDrag(dragInfo))
            {
                return;
            }

            // 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.
            var itemsControl = sender as ItemsControl;

            if ((Keyboard.Modifiers & ModifierKeys.Shift) == 0 && (Keyboard.Modifiers & ModifierKeys.Control) == 0 && dragInfo.VisualSourceItem != null && itemsControl != null && itemsControl.CanSelectMultipleItems())
            {
                var selectedItems = itemsControl.GetSelectedItems().OfType <object>().ToList();
                if (selectedItems.Count > 1 && selectedItems.Contains(dragInfo.SourceItem))
                {
                    m_ClickSupressItem = dragInfo.SourceItem;
                    e.Handled          = true;
                }
            }

            m_DragInfo = dragInfo;
        }
Esempio n. 6
0
        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;
                UIElement    item         = itemsControl.GetItemContainerAt(e.GetPosition(itemsControl));

                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++;
                            InTarget = 100 - ((e.GetPosition(item).Y / item.RenderSize.Height) * 100) >= 10;
                        }
                        else
                        {
                            InTarget = 0 + ((e.GetPosition(item).Y / item.RenderSize.Height) * 100) >= 10;
                        }
                    }
                    else
                    {
                        if (e.GetPosition(item).X > item.RenderSize.Width / 2)
                        {
                            InsertIndex++;
                            InTarget = 100 - ((e.GetPosition(item).Y / item.RenderSize.Width) * 100) >= 10;
                        }
                        else
                        {
                            InTarget = 0 + ((e.GetPosition(item).Y / item.RenderSize.Width) * 100) >= 10;
                        }
                    }
                }
                else
                {
                    TargetCollection = itemsControl.ItemsSource ?? itemsControl.Items;
                    InsertIndex      = itemsControl.Items.Count;
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Initializes a new instance of the DropInfo class.
        /// </summary>
        /// 
        /// <param name="sender">
        /// The sender of the drag event.
        /// </param>
        /// 
        /// <param name="e">
        /// The drag event.
        /// </param>
        /// 
        /// <param name="dragInfo">
        /// Information about the source of the drag, if the drag came from within the framework.
        /// </param>
        public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
        {
            string dataFormat = DragDrop.DataFormat.Name;
            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 position = e.GetPosition(itemsControl);
                UIElement item = itemsControl.GetItemContainerAt(position);
                bool directlyOverItem = item != null;

                TargetGroup = FindGroup(itemsControl, position);
                VisualTargetOrientation = itemsControl.GetItemsPanelOrientation();

                if (item == null)
                {
                    item = itemsControl.GetItemContainerAt(position, VisualTargetOrientation);
                    directlyOverItem = false;
                }

                if (item != null)
                {
                    ItemsControl itemParent = ItemsControl.ItemsControlFromItemContainer(item);

                    InsertIndex = itemParent.ItemContainerGenerator.IndexFromContainer(item);
                    TargetCollection = itemParent.ItemsSource ?? itemParent.Items;

                    if (directlyOverItem)
                    {
                        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;
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Gets the drag handler from the drag info or from the sender, if the drag info is null
        /// </summary>
        /// <param name="dragInfo">the drag info object</param>
        /// <param name="sender">the sender from an event, e.g. mouse down, mouse move</param>
        /// <returns></returns>
        private static IDragSource TryGetDragHandler(DragInfo dragInfo, UIElement sender)
        {
            IDragSource dragHandler = null;

            if (dragInfo != null && dragInfo.VisualSource != null)
            {
                dragHandler = GetDragHandler(dragInfo.VisualSource);
            }
            if (dragHandler == null && sender != null)
            {
                dragHandler = GetDragHandler(sender);
            }
            return(dragHandler ?? DefaultDragHandler);
        }
Esempio n. 9
0
        private static void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // Ignore the click if clickCount != 1 or the user has clicked on a scrollbar.
            var elementPosition = e.GetPosition((IInputElement)sender);

            if (e.ClickCount != 1 ||
                (sender as UIElement).IsDragSourceIgnored() ||
                (e.Source as UIElement).IsDragSourceIgnored() ||
                (e.OriginalSource as UIElement).IsDragSourceIgnored() ||
                HitTestUtilities.HitTest4Type <RangeBase>(sender, elementPosition) ||
                HitTestUtilities.HitTest4Type <ButtonBase>(sender, elementPosition) ||
                HitTestUtilities.HitTest4Type <TextBoxBase>(sender, elementPosition) ||
                HitTestUtilities.HitTest4Type <PasswordBox>(sender, elementPosition) ||
                HitTestUtilities.HitTest4Type <ComboBox>(sender, elementPosition) ||
                HitTestUtilities.HitTest4GridViewColumnHeader(sender, elementPosition) ||
                HitTestUtilities.HitTest4DataGridTypes(sender, elementPosition) ||
                HitTestUtilities.IsNotPartOfSender(sender, e))
            {
                m_DragInfo = null;
                return;
            }

            m_DragInfo = new DragInfo(sender, e);

            var dragHandler = TryGetDragHandler(m_DragInfo, sender as UIElement);

            if (!dragHandler.CanStartDrag(m_DragInfo))
            {
                m_DragInfo = null;
                return;
            }

            // 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.
            var itemsControl = sender as ItemsControl;

            if (m_DragInfo.VisualSourceItem != null && itemsControl != null && itemsControl.CanSelectMultipleItems())
            {
                var selectedItems = itemsControl.GetSelectedItems().Cast <object>();

                if (selectedItems.Count() > 1 && selectedItems.Contains(m_DragInfo.SourceItem))
                {
                    m_ClickSupressItem = m_DragInfo.SourceItem;
                    e.Handled          = true;
                }
            }
        }
Esempio n. 10
0
        public void StartDrag(DragInfo dragInfo)
        {
            var 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;
        }
Esempio n. 11
0
        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;
        }
Esempio n. 12
0
        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;
                UIElement item = itemsControl.GetItemContainerAt(e.GetPosition(itemsControl));

                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;
                }
            }
        }
        private static void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // Ignore the click if clickCount != 1 or the user has clicked on a scrollbar.
              var elementPosition = e.GetPosition((IInputElement)sender);
              if (e.ClickCount != 1
              || HitTestUtilities.HitTest4Type<ScrollBar>(sender, elementPosition)
              || HitTestUtilities.HitTest4Type<TextBoxBase>(sender, elementPosition)
              || HitTestUtilities.HitTest4Type<PasswordBox>(sender, elementPosition)
              || HitTestUtilities.HitTest4Type<Slider>(sender, elementPosition)
              || HitTestUtilities.HitTest4GridViewColumnHeader(sender, elementPosition)
              || HitTestUtilities.HitTest4DataGridTypes(sender, elementPosition)
              || GetDragSourceIgnore((UIElement)sender)) {
            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.
              var itemsControl = sender as ItemsControl;

              if (m_DragInfo.VisualSourceItem != null && itemsControl != null && itemsControl.CanSelectMultipleItems()) {
            var selectedItems = itemsControl.GetSelectedItems().Cast<object>();

            if (selectedItems.Count() > 1 && selectedItems.Contains(m_DragInfo.SourceItem)) {
              m_ClickSupressItem = m_DragInfo.SourceItem;
              e.Handled = true;
            }
              }
        }
        private 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.
              var 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;
        }
Esempio n. 15
0
        /// <summary>
        /// Initializes a new instance of the DropInfo class.
        /// </summary>
        /// 
        /// <param name="sender">
        /// The sender of the drag event.
        /// </param>
        /// 
        /// <param name="e">
        /// The drag event.
        /// </param>
        /// 
        /// <param name="dragInfo">
        /// Information about the source of the drag, if the drag came from within the framework.
        /// </param>
        public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
        {
            var dataFormat = DragDrop.DataFormat.Name;
            this.Data = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data;
            this.DragInfo = dragInfo;
            this.KeyStates = e.KeyStates;

            this.VisualTarget = sender as UIElement;
            this.DropPosition = e.GetPosition(this.VisualTarget);

            if (sender is ItemsControl)
            {
                var itemsControl = (ItemsControl)sender;
                var item = itemsControl.GetItemContainerAt(this.DropPosition);
                var directlyOverItem = item != null;

                this.TargetGroup = this.FindGroup(itemsControl, this.DropPosition);
                this.VisualTargetOrientation = itemsControl.GetItemsPanelOrientation();

                if (item == null)
                {
                    item = itemsControl.GetItemContainerAt(this.DropPosition, this.VisualTargetOrientation);
                    directlyOverItem = false;
                }

                if (item != null)
                {
                    var itemParent = ItemsControl.ItemsControlFromItemContainer(item);

                    this.InsertIndex = itemParent.ItemContainerGenerator.IndexFromContainer(item);
                    this.TargetCollection = itemParent.ItemsSource ?? itemParent.Items;

                    if (directlyOverItem)
                    {
                        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;
                }
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Initializes a new instance of the DropInfo class.
        /// </summary>
        ///
        /// <param name="sender">
        /// The sender of the drag event.
        /// </param>
        ///
        /// <param name="e">
        /// The drag event.
        /// </param>
        ///
        /// <param name="dragInfo">
        /// Information about the source of the drag, if the drag came from within the framework.
        /// </param>
        public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
        {
            var dataFormat = DragDrop.DataFormat.Name;

            this.Data      = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data;
            this.DragInfo  = dragInfo;
            this.KeyStates = e.KeyStates;

            this.VisualTarget = sender as UIElement;
            // if there is no drop target, find another
            if (!this.VisualTarget.IsDropTarget())
            {
                // try to find next element
                var element = this.VisualTarget.TryGetNextAncestorDropTargetElement();
                if (element != null)
                {
                    this.VisualTarget = element;
                }
            }
            // visual target can be null, so give us a point...
            this.DropPosition = this.VisualTarget != null?e.GetPosition(this.VisualTarget) : new Point();

            if (this.VisualTarget is ItemsControl)
            {
                var itemsControl = (ItemsControl)this.VisualTarget;
                item = itemsControl.GetItemContainerAt(this.DropPosition);
                var directlyOverItem = item != null;

                this.TargetGroup               = itemsControl.FindGroup(this.DropPosition);
                this.VisualTargetOrientation   = itemsControl.GetItemsPanelOrientation();
                this.VisualTargetFlowDirection = itemsControl.GetItemsPanelFlowDirection();

                if (item == null)
                {
                    item             = itemsControl.GetItemContainerAt(this.DropPosition, this.VisualTargetOrientation);
                    directlyOverItem = false;
                }

                if (item == null && this.TargetGroup != null && this.TargetGroup.IsBottomLevel)
                {
                    var itemData = this.TargetGroup.Items.FirstOrDefault();
                    if (itemData != null)
                    {
                        item             = itemsControl.ItemContainerGenerator.ContainerFromItem(itemData) as UIElement;
                        directlyOverItem = false;
                    }
                }

                if (item != null)
                {
                    itemParent = ItemsControl.ItemsControlFromItemContainer(item);

                    this.InsertIndex      = itemParent.ItemContainerGenerator.IndexFromContainer(item);
                    this.TargetCollection = itemParent.ItemsSource ?? itemParent.Items;

                    var tvItem = item as TreeViewItem;

                    if (directlyOverItem || tvItem != null)
                    {
                        this.TargetItem       = itemParent.ItemContainerGenerator.ItemFromContainer(item);
                        this.VisualTargetItem = item;
                    }

                    var itemRenderSize = item.RenderSize;

                    if (this.VisualTargetOrientation == Orientation.Vertical)
                    {
                        var currentYPos  = e.GetPosition(item).Y;
                        var targetHeight = itemRenderSize.Height;

                        if (currentYPos > targetHeight / 2)
                        {
                            this.InsertIndex++;
                            this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
                        }
                        else
                        {
                            this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
                        }

                        if (currentYPos > targetHeight * 0.25 && currentYPos < targetHeight * 0.75)
                        {
                            if (tvItem != null)
                            {
                                this.TargetCollection = tvItem.ItemsSource ?? tvItem.Items;
                                this.InsertIndex      = this.TargetCollection != null?this.TargetCollection.OfType <object>().Count() : 0;
                            }
                            this.InsertPosition |= RelativeInsertPosition.TargetItemCenter;
                        }
#if DEBUG
                        Console.WriteLine("==> DropInfo: {0}, {1}, {2}, Y={3}", this.InsertPosition, item, this.InsertIndex, currentYPos);
#endif
                    }
                    else
                    {
                        var currentXPos = e.GetPosition(item).X;
                        var targetWidth = itemRenderSize.Width;

                        if ((this.VisualTargetFlowDirection == FlowDirection.RightToLeft && currentXPos < targetWidth / 2) ||
                            (this.VisualTargetFlowDirection == FlowDirection.LeftToRight && currentXPos > targetWidth / 2))
                        {
                            this.InsertIndex++;
                            this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
                        }
                        else
                        {
                            this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
                        }

                        if (currentXPos > targetWidth * 0.25 && currentXPos < targetWidth * 0.75)
                        {
                            if (tvItem != null)
                            {
                                this.TargetCollection = tvItem.ItemsSource ?? tvItem.Items;
                                this.InsertIndex      = this.TargetCollection != null?this.TargetCollection.OfType <object>().Count() : 0;
                            }
                            this.InsertPosition |= RelativeInsertPosition.TargetItemCenter;
                        }
#if DEBUG
                        Console.WriteLine("==> DropInfo: {0}, {1}, {2}, X={3}", this.InsertPosition, item, this.InsertIndex, currentXPos);
#endif
                    }
                }
                else
                {
                    this.TargetCollection = itemsControl.ItemsSource ?? itemsControl.Items;
                    this.InsertIndex      = itemsControl.Items.Count;
#if DEBUG
                    Console.WriteLine("==> DropInfo: {0}, item=NULL, {1}", this.InsertPosition, this.InsertIndex);
#endif
                }
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Initializes a new instance of the DropInfo class.
        /// </summary>
        /// 
        /// <param name="sender">
        /// The sender of the drag event.
        /// </param>
        /// 
        /// <param name="e">
        /// The drag event.
        /// </param>
        /// 
        /// <param name="dragInfo">
        /// Information about the source of the drag, if the drag came from within the framework.
        /// </param>
        public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
        {
            var dataFormat = DragDrop.DataFormat.Name;
            this.Data = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data;
            this.DragInfo = dragInfo;
            this.KeyStates = e.KeyStates;

            this.VisualTarget = sender as UIElement;
            this.DropPosition = e.GetPosition(this.VisualTarget);

            if (sender is ItemsControl)
            {
                var itemsControl = (ItemsControl)sender;
                var item = itemsControl.GetItemContainerAt(this.DropPosition);
                var directlyOverItem = item != null;

                this.TargetGroup = this.FindGroup(itemsControl, this.DropPosition);
                this.VisualTargetOrientation = itemsControl.GetItemsPanelOrientation();
                this.VisualTargetFlowDirection = itemsControl.GetItemsPanelFlowDirection();

                if (item == null)
                {
                    item = itemsControl.GetItemContainerAt(this.DropPosition, this.VisualTargetOrientation);
                    directlyOverItem = false;
                }

                if (item != null)
                {
                    var itemParent = ItemsControl.ItemsControlFromItemContainer(item);

                    this.InsertIndex = itemParent.ItemContainerGenerator.IndexFromContainer(item);
                    this.TargetCollection = itemParent.ItemsSource ?? itemParent.Items;

                    if (directlyOverItem || typeof(TreeViewItem).IsAssignableFrom(item.GetType()))
                    {
                        this.TargetItem = itemParent.ItemContainerGenerator.ItemFromContainer(item);
                        this.VisualTargetItem = item;
                    }

                    var itemRenderSize = item.RenderSize;

                    if (this.VisualTargetOrientation == Orientation.Vertical)
                    {
                        var currentYPos = e.GetPosition(item).Y;
                        var targetHeight = itemRenderSize.Height;

                        if (currentYPos > targetHeight / 2)
                        {
                            InsertIndex++;
                            InsertPosition = RelativeInsertPosition.AfterTargetItem;
                        }
                        else
                        {
                            InsertPosition = RelativeInsertPosition.BeforeTargetItem;
                        }

                        if (currentYPos > targetHeight * 0.25 && currentYPos < targetHeight * 0.75)
                        {
                            InsertPosition |= RelativeInsertPosition.TargetItemCenter;
                        }
                    }
                    else
                    {
                        var currentXPos = e.GetPosition(item).X;
                        var targetWidth = itemRenderSize.Width;

                        if ((this.VisualTargetFlowDirection == FlowDirection.RightToLeft && currentXPos < targetWidth / 2)
                            || (this.VisualTargetFlowDirection == FlowDirection.LeftToRight && currentXPos > targetWidth / 2))
                        {
                            InsertIndex++;
                            InsertPosition = RelativeInsertPosition.AfterTargetItem;
                        }
                        else
                        {
                            InsertPosition = RelativeInsertPosition.BeforeTargetItem;
                        }

                        if (currentXPos > targetWidth * 0.25 && currentXPos < targetWidth * 0.75)
                        {
                            InsertPosition |= RelativeInsertPosition.TargetItemCenter;
                        }
            #if DEBUG
                        Console.WriteLine("==> DropInfo: {0}, {1}, {2}, X={3}", InsertPosition, item, InsertIndex, currentXPos);
            #endif
                    }
                }
                else
                {
                    this.TargetCollection = itemsControl.ItemsSource ?? itemsControl.Items;
                    this.InsertIndex = itemsControl.Items.Count;
                }
            }
        }
Esempio n. 18
0
        static void DragSource_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //this only fires if we didn't initiate a drag.  So this is where we need to re-raise the
            //preview down event.
            if (m_DragInfo != null)
            {
                UIElement uiElement = (UIElement)sender;

                //Every RoutedEventArgs must have a non-null RoutedEvent associated with it.
                int ts = new TimeSpan(DateTime.Now.Ticks).Milliseconds;
                MouseButtonEventArgs mbargs = new MouseButtonEventArgs(Mouse.PrimaryDevice, ts, MouseButton.Left);
                //mbargs.Source = uiElement;
                mbargs.RoutedEvent = UIElement.PreviewMouseLeftButtonDownEvent;
                uiElement.RaiseEvent(mbargs);
                // TODO: Re-raise the supressed event if the user didn't initiate a drag.

                /*if (uiElement.PreviewMouseLeftButtonDown != null)
                {
                    uiElement.PreviewMouseLeftButtonDown(sender, e);
                }*/

                m_DragInfo = null;
            }
        }
Esempio n. 19
0
        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 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;
                }
            }
        }
Esempio n. 20
0
    private static void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      // Ignore the click if clickCount != 1 or the user has clicked on a scrollbar.
      var elementPosition = e.GetPosition((IInputElement)sender);
      if (e.ClickCount != 1
          || (sender as UIElement).IsDragSourceIgnored()
          || (e.Source as UIElement).IsDragSourceIgnored()
          || (e.OriginalSource as UIElement).IsDragSourceIgnored()
          || (sender is TabControl) && !HitTestUtilities.HitTest4Type<TabPanel>(sender, elementPosition)
          || HitTestUtilities.HitTest4Type<RangeBase>(sender, elementPosition)
          || HitTestUtilities.HitTest4Type<ButtonBase>(sender, elementPosition)
          || HitTestUtilities.HitTest4Type<TextBoxBase>(sender, elementPosition)
          || HitTestUtilities.HitTest4Type<PasswordBox>(sender, elementPosition)
          || HitTestUtilities.HitTest4Type<ComboBox>(sender, elementPosition)
          || HitTestUtilities.HitTest4GridViewColumnHeader(sender, elementPosition)
          || HitTestUtilities.HitTest4DataGridTypes(sender, elementPosition)
          || HitTestUtilities.IsNotPartOfSender(sender, e)) {
        m_DragInfo = null;
        return;
      }

      m_DragInfo = new DragInfo(sender, e);

      var dragHandler = TryGetDragHandler(m_DragInfo, sender as UIElement);
      if (!dragHandler.CanStartDrag(m_DragInfo)) {
        m_DragInfo = null;
        return;
      }

      // 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.
      var itemsControl = sender as ItemsControl;
      if ((Keyboard.Modifiers & ModifierKeys.Shift) == 0 && (Keyboard.Modifiers & ModifierKeys.Control) == 0 && m_DragInfo.VisualSourceItem != null && itemsControl != null && itemsControl.CanSelectMultipleItems()) {
        var selectedItems = itemsControl.GetSelectedItems().OfType<object>().ToList();
        if (selectedItems.Count > 1 && selectedItems.Contains(m_DragInfo.SourceItem)) {
          m_ClickSupressItem = m_DragInfo.SourceItem;
          e.Handled = true;
        }
      }
    }
Esempio n. 21
0
        static void DragSource_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (m_DragInfo != null)
            {
                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(m_Format.Name, m_DragInfo.Data);
                        System.Windows.DragDrop.DoDragDrop(m_DragInfo.VisualSource, data, m_DragInfo.Effects);
                        m_DragInfo = null;
                    }
                }
            }
        }
Esempio n. 22
0
        /// <summary>
        /// Initializes a new instance of the DropInfo class.
        /// </summary>
        /// 
        /// <param name="sender">
        /// The sender of the drag event.
        /// </param>
        /// 
        /// <param name="e">
        /// The drag event.
        /// </param>
        /// 
        /// <param name="dragInfo">
        /// Information about the source of the drag, if the drag came from within the framework.
        /// </param>
        public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
        {
            var dataFormat = DragDrop.DataFormat.Name;
              this.Data = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data;
              this.DragInfo = dragInfo;
              this.KeyStates = e.KeyStates;

              this.VisualTarget = sender as UIElement;
              // if there is no drop target, find another
              if (!this.VisualTarget.IsDropTarget())
              {
            // try to find next element
            var element = this.VisualTarget.TryGetNextAncestorDropTargetElement();
            if (element != null)
            {
              this.VisualTarget = element;
            }
              }
              // visual target can be null, so give us a point...
              this.DropPosition = this.VisualTarget != null ? e.GetPosition(this.VisualTarget) : new Point();

              if (this.VisualTarget is TabControl) {
            if (!HitTestUtilities.HitTest4Type<TabPanel>(this.VisualTarget, this.DropPosition)) {
              return;
            }
              }

              if (this.VisualTarget is ItemsControl) {
            var itemsControl = (ItemsControl)this.VisualTarget;
            item = itemsControl.GetItemContainerAt(this.DropPosition);
            if (item != null && item.GetVisualAncestor<ItemsControl>() != itemsControl)
               item = null;
            var directlyOverItem = item != null;

            this.TargetGroup = itemsControl.FindGroup(this.DropPosition);
            this.VisualTargetOrientation = itemsControl.GetItemsPanelOrientation();
            this.VisualTargetFlowDirection = itemsControl.GetItemsPanelFlowDirection();

            if (item == null) {
              try
              {
            item = itemsControl.GetItemContainerAt(this.DropPosition, this.VisualTargetOrientation);
            directlyOverItem = false;
              }
              catch(Exception)
              { }
            }

            if (item == null && this.TargetGroup != null && this.TargetGroup.IsBottomLevel)
            {
              var itemData = this.TargetGroup.Items.FirstOrDefault();
              if (itemData != null)
              {
            item = itemsControl.ItemContainerGenerator.ContainerFromItem(itemData) as UIElement;
            directlyOverItem = false;
              }
            }

            if (item != null) {
              itemParent = ItemsControl.ItemsControlFromItemContainer(item);

              this.InsertIndex = itemParent.ItemContainerGenerator.IndexFromContainer(item);
              this.TargetCollection = itemParent.ItemsSource ?? itemParent.Items;

              var tvItem = item as TreeViewItem;

              if (directlyOverItem || tvItem != null)
              {
            this.TargetItem = itemParent.ItemContainerGenerator.ItemFromContainer(item);
            this.VisualTargetItem = item;
              }

              var itemRenderSize = item.RenderSize;

              if (this.VisualTargetOrientation == Orientation.Vertical) {
            var currentYPos = e.GetPosition(item).Y;
            var targetHeight = itemRenderSize.Height;

            if (currentYPos > targetHeight / 2) {
              this.InsertIndex++;
              this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
            } else {
              this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
            }

            if (currentYPos > targetHeight * 0.25 && currentYPos < targetHeight * 0.75) {
              if (tvItem != null)
              {
                this.TargetCollection = tvItem.ItemsSource ?? tvItem.Items;
                this.InsertIndex = this.TargetCollection != null ? this.TargetCollection.OfType<object>().Count() : 0;
              }
              this.InsertPosition |= RelativeInsertPosition.TargetItemCenter;
            }
            //System.Diagnostics.Debug.WriteLine("==> DropInfo: {0}, {1}, {2}, Y={3}", this.InsertPosition, item, this.InsertIndex, currentYPos);
              }
              else {
            var currentXPos = e.GetPosition(item).X;
            var targetWidth = itemRenderSize.Width;

            if (this.VisualTargetFlowDirection == FlowDirection.RightToLeft) {
              if (currentXPos > targetWidth / 2) {
                this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
              } else {
                this.InsertIndex++;
                this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
              }
            } else if (this.VisualTargetFlowDirection == FlowDirection.LeftToRight) {
              if (currentXPos > targetWidth / 2) {
                this.InsertIndex++;
                this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
              } else {
                this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
              }
            }

            if (currentXPos > targetWidth * 0.25 && currentXPos < targetWidth * 0.75) {
              if (tvItem != null)
              {
                this.TargetCollection = tvItem.ItemsSource ?? tvItem.Items;
                this.InsertIndex = this.TargetCollection != null ? this.TargetCollection.OfType<object>().Count() : 0;
              }
              this.InsertPosition |= RelativeInsertPosition.TargetItemCenter;
            }
            //System.Diagnostics.Debug.WriteLine("==> DropInfo: InsPos={0}, InsIndex={1}, X={2}, Item={3}", this.InsertPosition, this.InsertIndex, currentXPos, item);
              }
            }
            else
            {
              this.TargetCollection = itemsControl.ItemsSource ?? itemsControl.Items;
              this.InsertIndex = itemsControl.Items.Count;
              //System.Diagnostics.Debug.WriteLine("==> DropInfo: {0}, item=NULL, {1}", this.InsertPosition, this.InsertIndex);
            }
              } else {
            this.VisualTargetItem = this.VisualTarget;
              }
        }
Esempio n. 23
0
        private static void DragSource_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (m_DragInfo != null && !m_DragInProgress) {

            // do nothing if mouse left button is released or the pointer is captured
            if (e.LeftButton == MouseButtonState.Released || ((IInputElement)e.OriginalSource).IsMouseCaptured)
            {
              m_DragInfo = null;
              return;
            }

            var dragStart = m_DragInfo.DragStartPosition;
            var position = e.GetPosition((IInputElement)sender);

            if (Math.Abs(position.X - dragStart.X) > SystemParameters.MinimumHorizontalDragDistance ||
            Math.Abs(position.Y - dragStart.Y) > SystemParameters.MinimumVerticalDragDistance) {
              var dragHandler = TryGetDragHandler(m_DragInfo, sender as UIElement);
              if (dragHandler.CanStartDrag(m_DragInfo)) {
            dragHandler.StartDrag(m_DragInfo);

            if (m_DragInfo.Effects != DragDropEffects.None && m_DragInfo.Data != null) {
              var data = m_DragInfo.DataObject;

              if (data == null) {
                data = new DataObject(DataFormat.Name, m_DragInfo.Data);
              } else {
                data.SetData(DataFormat.Name, m_DragInfo.Data);
              }

              try {
                m_DragInProgress = true;
                var result = System.Windows.DragDrop.DoDragDrop(m_DragInfo.VisualSource, data, m_DragInfo.Effects);
                if (result == DragDropEffects.None)
                  dragHandler.DragCancelled();
              }
              finally {
                m_DragInProgress = false;
              }

              m_DragInfo = null;
            }
              }
            }
              }
        }
        /// <summary>
        /// Initializes a new instance of the DropInfo class.
        /// </summary>
        ///
        /// <param name="sender">
        /// The sender of the drag event.
        /// </param>
        ///
        /// <param name="e">
        /// The drag event.
        /// </param>
        ///
        /// <param name="dragInfo">
        /// Information about the source of the drag, if the drag came from within the framework.
        /// </param>
        public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
        {
            var dataFormat = DragDrop.DataFormat.Name;

            this.Data      = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data;
            this.DragInfo  = dragInfo;
            this.KeyStates = e.KeyStates;

            this.VisualTarget = sender as UIElement;
            // if drop target isn't a ItemsControl
            if (!(this.VisualTarget is ItemsControl))
            {
                // try to find next ItemsControl
                var itemsControl = VisualTreeExtensions.GetVisualAncestor <ItemsControl>(this.VisualTarget);
                if (itemsControl != null)
                {
                    // now check if this ItemsControl is a drop target
                    if (DragDrop.GetIsDropTarget(itemsControl))
                    {
                        this.VisualTarget = itemsControl;
                    }
                }
            }
            // visual target can be null, so give us a point...
            this.DropPosition = this.VisualTarget != null?e.GetPosition(this.VisualTarget) : new Point();

            if (this.VisualTarget is ItemsControl)
            {
                var itemsControl     = (ItemsControl)this.VisualTarget;
                var item             = itemsControl.GetItemContainerAt(this.DropPosition);
                var directlyOverItem = item != null;

                this.TargetGroup               = this.FindGroup(itemsControl, this.DropPosition);
                this.VisualTargetOrientation   = itemsControl.GetItemsPanelOrientation();
                this.VisualTargetFlowDirection = itemsControl.GetItemsPanelFlowDirection();

                if (item == null)
                {
                    item             = itemsControl.GetItemContainerAt(this.DropPosition, this.VisualTargetOrientation);
                    directlyOverItem = false;
                }

                if (item != null)
                {
                    var itemParent = ItemsControl.ItemsControlFromItemContainer(item);

                    this.InsertIndex      = itemParent.ItemContainerGenerator.IndexFromContainer(item);
                    this.TargetCollection = itemParent.ItemsSource ?? itemParent.Items;

                    if (directlyOverItem || typeof(TreeViewItem).IsAssignableFrom(item.GetType()))
                    {
                        this.TargetItem       = itemParent.ItemContainerGenerator.ItemFromContainer(item);
                        this.VisualTargetItem = item;
                    }

                    var itemRenderSize = item.RenderSize;

                    if (this.VisualTargetOrientation == Orientation.Vertical)
                    {
                        var currentYPos  = e.GetPosition(item).Y;
                        var targetHeight = itemRenderSize.Height;

                        if (currentYPos > targetHeight / 2)
                        {
                            this.InsertIndex++;
                            this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
                        }
                        else
                        {
                            this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
                        }

                        if (currentYPos > targetHeight * 0.25 && currentYPos < targetHeight * 0.75)
                        {
                            this.InsertPosition |= RelativeInsertPosition.TargetItemCenter;
                        }
                    }
                    else
                    {
                        var currentXPos = e.GetPosition(item).X;
                        var targetWidth = itemRenderSize.Width;

                        if ((this.VisualTargetFlowDirection == FlowDirection.RightToLeft && currentXPos < targetWidth / 2) ||
                            (this.VisualTargetFlowDirection == FlowDirection.LeftToRight && currentXPos > targetWidth / 2))
                        {
                            this.InsertIndex++;
                            this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
                        }
                        else
                        {
                            this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
                        }

                        if (currentXPos > targetWidth * 0.25 && currentXPos < targetWidth * 0.75)
                        {
                            this.InsertPosition |= RelativeInsertPosition.TargetItemCenter;
                        }
#if DEBUG
                        Console.WriteLine("==> DropInfo: {0}, {1}, {2}, X={3}", this.InsertPosition, item, this.InsertIndex, currentXPos);
#endif
                    }
                }
                else
                {
                    this.TargetCollection = itemsControl.ItemsSource ?? itemsControl.Items;
                    this.InsertIndex      = itemsControl.Items.Count;
                }
            }
        }
Esempio n. 25
0
        private static void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // Ignore the click if clickCount != 1 or the user has clicked on a scrollbar.
            if (e.ClickCount != 1 || 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.
            var itemsControl = sender as ItemsControl;

            if (m_DragInfo.VisualSourceItem != null && itemsControl != null && itemsControl.CanSelectMultipleItems())
            {
                var selectedItems = itemsControl.GetSelectedItems().Cast<object>();

                if (selectedItems.Count() > 1 && selectedItems.Contains(m_DragInfo.SourceItem))
                {
                    m_ClickSupressItem = m_DragInfo.SourceItem;
                    e.Handled = true;
                }
            }
        }
        private static void DragSource_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (m_DragInfo != null && !m_DragInProgress) {
            var dragStart = m_DragInfo.DragStartPosition;
            var position = e.GetPosition((IInputElement)sender);

            if (Math.Abs(position.X - dragStart.X) > SystemParameters.MinimumHorizontalDragDistance ||
            Math.Abs(position.Y - dragStart.Y) > SystemParameters.MinimumVerticalDragDistance) {
              var dragHandler = TryGetDragHandler(m_DragInfo, sender as UIElement);
              dragHandler.StartDrag(m_DragInfo);

              if (m_DragInfo.Effects != DragDropEffects.None && m_DragInfo.Data != null) {
            var data = m_DragInfo.DataObject;

            if (data == null) {
              data = new DataObject(DataFormat.Name, m_DragInfo.Data);
            } else {
              data.SetData(DataFormat.Name, m_DragInfo.Data);
            }

            try {
              m_DragInProgress = true;
              var result = System.Windows.DragDrop.DoDragDrop(m_DragInfo.VisualSource, data, m_DragInfo.Effects);
              if (result == DragDropEffects.None) dragHandler.DragCancelled();
            } finally {
              m_DragInProgress = false;
            }

            m_DragInfo = null;
              }
            }
              }
        }
Esempio n. 27
0
        private static void DragSource_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var elementPosition = e.GetPosition((IInputElement)sender);
              if ((sender is TabControl) && !HitTestUtilities.HitTest4Type<TabPanel>(sender, elementPosition)) {
            m_DragInfo = null;
            m_ClickSupressItem = null;
            return;
              }

              // 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.
              var itemsControl = sender as ItemsControl;
              if (itemsControl != null && m_DragInfo != null && m_ClickSupressItem != null && m_ClickSupressItem == m_DragInfo.SourceItem) {
            if ((Keyboard.Modifiers & ModifierKeys.Control) != 0) {
              itemsControl.SetItemSelected(m_DragInfo.SourceItem, false);
            } else if ((Keyboard.Modifiers & ModifierKeys.Shift) == 0) {
              itemsControl.SetSelectedItem(m_DragInfo.SourceItem);
            }
              }

              m_DragInfo = null;
              m_ClickSupressItem = null;
        }
 private static IDragSource TryGetDragHandler(DragInfo dragInfo, UIElement sender)
 {
     IDragSource dragHandler = null;
       if (dragInfo != null && dragInfo.VisualSource != null) {
     dragHandler = GetDragHandler(m_DragInfo.VisualSource);
       }
       if (dragHandler == null && sender != null) {
     dragHandler = GetDragHandler(sender);
       }
       return dragHandler ?? DefaultDragHandler;
 }
Esempio n. 29
0
        private static void DragSource_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (m_DragInfo != null && !m_DragInProgress)
            {
                var dragStart = m_DragInfo.DragStartPosition;
                var position = e.GetPosition(null);

                if (Math.Abs(position.X - dragStart.X) > SystemParameters.MinimumHorizontalDragDistance ||
                    Math.Abs(position.Y - dragStart.Y) > SystemParameters.MinimumVerticalDragDistance)
                {
                    var 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)
                    {
                        var 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;
                    }
                }
            }
        }
Esempio n. 30
0
        /// <summary>
        /// Initializes a new instance of the DropInfo class.
        /// </summary>
        ///
        /// <param name="sender">
        /// The sender of the drag event.
        /// </param>
        ///
        /// <param name="e">
        /// The drag event.
        /// </param>
        ///
        /// <param name="dragInfo">
        /// Information about the source of the drag, if the drag came from within the framework.
        /// </param>
        public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
        {
            var dataFormat = DragDrop.DataFormat.Name;

            this.Data      = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data;
            this.DragInfo  = dragInfo;
            this.KeyStates = e.KeyStates;

            this.VisualTarget = sender as UIElement;
            // if there is no drop target, find another
            if (!this.VisualTarget.IsDropTarget())
            {
                // try to find next element
                var element = this.VisualTarget.TryGetNextAncestorDropTargetElement();
                if (element != null)
                {
                    this.VisualTarget = element;
                }
            }

            // try find ScrollViewer
            if (this.VisualTarget is TabControl)
            {
                var tabPanel = this.VisualTarget.GetVisualDescendent <TabPanel>();
                this.TargetScrollViewer = tabPanel?.GetVisualAncestor <ScrollViewer>();
            }
            else
            {
                this.TargetScrollViewer = this.VisualTarget?.GetVisualDescendent <ScrollViewer>();
            }

            // visual target can be null, so give us a point...
            this.DropPosition = this.VisualTarget != null?e.GetPosition(this.VisualTarget) : new Point();

            if (this.VisualTarget is TabControl)
            {
                if (!HitTestUtilities.HitTest4Type <TabPanel>(this.VisualTarget, this.DropPosition))
                {
                    return;
                }
            }

            if (this.VisualTarget is ItemsControl)
            {
                var itemsControl = (ItemsControl)this.VisualTarget;
                //System.Diagnostics.Debug.WriteLine(">>> Name = {0}", itemsControl.Name);
                // get item under the mouse
                item = itemsControl.GetItemContainerAt(this.DropPosition);
                var directlyOverItem = item != null;

                this.TargetGroup               = itemsControl.FindGroup(this.DropPosition);
                this.VisualTargetOrientation   = itemsControl.GetItemsPanelOrientation();
                this.VisualTargetFlowDirection = itemsControl.GetItemsPanelFlowDirection();

                if (item == null)
                {
                    // ok, no item found, so maybe we can found an item at top, left, right or bottom
                    item             = itemsControl.GetItemContainerAt(this.DropPosition, this.VisualTargetOrientation);
                    directlyOverItem = false;
                }

                if (item == null && this.TargetGroup != null && this.TargetGroup.IsBottomLevel)
                {
                    var itemData = this.TargetGroup.Items.FirstOrDefault();
                    if (itemData != null)
                    {
                        item             = itemsControl.ItemContainerGenerator.ContainerFromItem(itemData) as UIElement;
                        directlyOverItem = false;
                    }
                }

                if (item != null)
                {
                    itemParent = ItemsControl.ItemsControlFromItemContainer(item);
                    this.VisualTargetOrientation   = itemParent.GetItemsPanelOrientation();
                    this.VisualTargetFlowDirection = itemParent.GetItemsPanelFlowDirection();

                    this.InsertIndex      = itemParent.ItemContainerGenerator.IndexFromContainer(item);
                    this.TargetCollection = itemParent.ItemsSource ?? itemParent.Items;

                    var tvItem = item as TreeViewItem;

                    if (directlyOverItem || tvItem != null)
                    {
                        this.TargetItem       = itemParent.ItemContainerGenerator.ItemFromContainer(item);
                        this.VisualTargetItem = item;
                    }

                    var itemRenderSize = item.RenderSize;

                    if (this.VisualTargetOrientation == Orientation.Vertical)
                    {
                        var currentYPos  = e.GetPosition(item).Y;
                        var targetHeight = itemRenderSize.Height;

                        if (currentYPos > targetHeight / 2)
                        {
                            this.InsertIndex++;
                            this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
                        }
                        else
                        {
                            this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
                        }

                        if (currentYPos > targetHeight * 0.25 && currentYPos < targetHeight * 0.75)
                        {
                            if (tvItem != null)
                            {
                                this.TargetCollection = tvItem.ItemsSource ?? tvItem.Items;
                                this.InsertIndex      = this.TargetCollection != null?this.TargetCollection.OfType <object>().Count() : 0;
                            }
                            this.InsertPosition |= RelativeInsertPosition.TargetItemCenter;
                        }
                        //System.Diagnostics.Debug.WriteLine("==> DropInfo: pos={0}, idx={1}, Y={2}, Item={3}", this.InsertPosition, this.InsertIndex, currentYPos, item);
                    }
                    else
                    {
                        var currentXPos = e.GetPosition(item).X;
                        var targetWidth = itemRenderSize.Width;

                        if (this.VisualTargetFlowDirection == FlowDirection.RightToLeft)
                        {
                            if (currentXPos > targetWidth / 2)
                            {
                                this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
                            }
                            else
                            {
                                this.InsertIndex++;
                                this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
                            }
                        }
                        else if (this.VisualTargetFlowDirection == FlowDirection.LeftToRight)
                        {
                            if (currentXPos > targetWidth / 2)
                            {
                                this.InsertIndex++;
                                this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
                            }
                            else
                            {
                                this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
                            }
                        }

                        if (currentXPos > targetWidth * 0.25 && currentXPos < targetWidth * 0.75)
                        {
                            if (tvItem != null)
                            {
                                this.TargetCollection = tvItem.ItemsSource ?? tvItem.Items;
                                this.InsertIndex      = this.TargetCollection != null?this.TargetCollection.OfType <object>().Count() : 0;
                            }
                            this.InsertPosition |= RelativeInsertPosition.TargetItemCenter;
                        }
                        //System.Diagnostics.Debug.WriteLine("==> DropInfo: pos={0}, idx={1}, X={2}, Item={3}", this.InsertPosition, this.InsertIndex, currentXPos, item);
                    }
                }
                else
                {
                    this.TargetCollection = itemsControl.ItemsSource ?? itemsControl.Items;
                    this.InsertIndex      = itemsControl.Items.Count;
                    //System.Diagnostics.Debug.WriteLine("==> DropInfo: pos={0}, item=NULL, idx={1}", this.InsertPosition, this.InsertIndex);
                }
            }
            else
            {
                this.VisualTargetItem = this.VisualTarget;
            }
        }
Esempio n. 31
0
 static void DragSource_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
 {
     if (m_DragInfo != null)
     {
         m_DragInfo = null;
     }
 }
Esempio n. 32
0
		/// <summary>
		/// Initializes a new instance of the DropInfo class.
		/// </summary>
		///
		/// <param name="sender">
		/// The sender of the drag event.
		/// </param>
		///
		/// <param name="e">
		/// The drag event.
		/// </param>
		///
		/// <param name="dragInfo">
		/// Information about the source of the drag, if the drag came from within the framework.
		/// </param>
		public DropInfo(object sender, DragEventArgs e, DragInfo dragInfo)
		{
			var dataFormat = DragDrop.DataFormat.Name;

			this.Data = (e.Data.GetDataPresent(dataFormat)) ? e.Data.GetData(dataFormat) : e.Data;
			this.DragInfo = dragInfo;
			this.KeyStates = e.KeyStates;

			this.VisualTarget = sender as UIElement;
			// if drop target isn't a ItemsControl
			if (!(this.VisualTarget is ItemsControl))
			{
				// try to find next ItemsControl
				var itemsControl = VisualTreeExtensions.GetVisualAncestor<ItemsControl>(this.VisualTarget);
				if (itemsControl != null)
				{
					// now check if this ItemsControl is a drop target
					if (DragDrop.GetIsDropTarget(itemsControl))
					{
						this.VisualTarget = itemsControl;
					}
				}
			}
			// visual target can be null, so give us a point...
			this.DropPosition = this.VisualTarget != null ? e.GetPosition(this.VisualTarget) : new Point();

			if (this.VisualTarget is ItemsControl)
			{
				var itemsControl = (ItemsControl)this.VisualTarget;
				var item = itemsControl.GetItemContainerAt(this.DropPosition);
				var directlyOverItem = item != null;

				this.TargetGroup = itemsControl.FindGroup(this.DropPosition);
				this.VisualTargetOrientation = itemsControl.GetItemsPanelOrientation();
				this.VisualTargetFlowDirection = itemsControl.GetItemsPanelFlowDirection();

				if (item == null)
				{
					item = itemsControl.GetItemContainerAt(this.DropPosition, this.VisualTargetOrientation);
					directlyOverItem = false;
				}

				if (item != null)
				{
					var itemParent = ItemsControl.ItemsControlFromItemContainer(item);

					this.InsertIndex = itemParent.ItemContainerGenerator.IndexFromContainer(item);
					this.TargetCollection = itemParent.ItemsSource ?? itemParent.Items;

					if (directlyOverItem || typeof(TreeViewItem).IsAssignableFrom(item.GetType()))
					{
						this.TargetItem = itemParent.ItemContainerGenerator.ItemFromContainer(item);
						this.VisualTargetItem = item;
					}

					var itemRenderSize = item.RenderSize;

					if (this.VisualTargetOrientation == Orientation.Vertical)
					{
						var currentYPos = e.GetPosition(item).Y;
						var targetHeight = itemRenderSize.Height;

						if (currentYPos > targetHeight / 2)
						{
							this.InsertIndex++;
							this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
						}
						else
						{
							this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
						}

						if (currentYPos > targetHeight * 0.25 && currentYPos < targetHeight * 0.75)
						{
							this.InsertPosition |= RelativeInsertPosition.TargetItemCenter;
						}
					}
					else
					{
						var currentXPos = e.GetPosition(item).X;
						var targetWidth = itemRenderSize.Width;

						if ((this.VisualTargetFlowDirection == FlowDirection.RightToLeft && currentXPos < targetWidth / 2)
								|| (this.VisualTargetFlowDirection == FlowDirection.LeftToRight && currentXPos > targetWidth / 2))
						{
							this.InsertIndex++;
							this.InsertPosition = RelativeInsertPosition.AfterTargetItem;
						}
						else
						{
							this.InsertPosition = RelativeInsertPosition.BeforeTargetItem;
						}

						if (currentXPos > targetWidth * 0.25 && currentXPos < targetWidth * 0.75)
						{
							this.InsertPosition |= RelativeInsertPosition.TargetItemCenter;
						}
#if DEBUG
						Console.WriteLine("==> DropInfo: {0}, {1}, {2}, X={3}", this.InsertPosition, item, this.InsertIndex, currentXPos);
#endif
					}
				}
				else
				{
					this.TargetCollection = itemsControl.ItemsSource ?? itemsControl.Items;
					this.InsertIndex = itemsControl.Items.Count;
				}
			}
		}
 public void StartDrag(DragInfo dragInfo)
 {
     //throw new NotImplementedException();
 }