示例#1
0
        /// <summary>
        /// This method returns null if no DragDropTarget is under the pointer, else it returns the DragDropTarget under it (the first Parent found)
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="itemContainerUnderPointer">The ListBoxItem or other container that is being moved. If there is no container, it is the item directly.</param>
        /// <returns>Null if no DragDropTarget is under the pointer, else it returns the DragDropTarget under it (the first Parent found)</returns>
        static DragDropTarget <TItemsControlType, TItemContainerType> GetDragDropTargetUnderPointer(double x, double y, out TItemContainerType itemContainerUnderPointer)
        {
            UIElement element             = VisualTreeHelper.FindElementInHostCoordinates(new Point(x, y));
            UIElement childElement        = null;
            UIElement childOfChildElement = null;

            while (element != null)
            {
                if (element is DragDropTarget <TItemsControlType, TItemContainerType> )
                {
                    //------------------
                    // FOUND
                    //------------------
                    DragDropTarget <TItemsControlType, TItemContainerType> dragDropTargetUnder = (DragDropTarget <TItemsControlType, TItemContainerType>)element;
                    itemContainerUnderPointer = childOfChildElement as TItemContainerType;
                    return(dragDropTargetUnder);
                }

                //Move up to the parent
                childOfChildElement = childElement;
                childElement        = element;
                element             = (UIElement)element.INTERNAL_VisualParent;
            }

            // Not found:
            itemContainerUnderPointer = null;
            return(null);
        }
        /// <summary>
        /// Determines whether an element is visible on screen, and if it is the foremost element (ie. it is not covered by another element). This is useful for example to hide the TextBox validation tooltips when they become hidden after scrolling (cf. ZenDesk 628).
        /// </summary>
        /// <param name="element">The element to check.</param>
        /// <returns>Returns True if the element is visible on screen and it is the foremost element, False otherwise.</returns>
        internal static bool IsElementVisibleOnScreen(FrameworkElement element)
        {
            // First, verify that the element is in the visual tree:
            if (!element._isLoaded)
            {
                return(false);
            }

            // Then, check whether the element is visible in the DOM tree, regardless of elements that may cover it. This can be false if at least one of the parents has "Visibility=Collapsed".
            if (!INTERNAL_VisibilityChangedNotifier.IsElementVisible(element))
            {
                return(false);
            }

            // Verify that the size of the element can be read:
            Size actualSize = element.INTERNAL_GetActualWidthAndHeight();

            if (double.IsNaN(actualSize.Width) || double.IsNaN(actualSize.Height))
            {
                return(false);
            }

            // Get the bounds of the element:
            Point topLeftCoordinates = INTERNAL_PopupsManager.GetUIElementAbsolutePosition(element);
            Rect  elementBounds      = new Rect(topLeftCoordinates.X, topLeftCoordinates.Y, actualSize.Width, actualSize.Height);

            // Get the bounds of the window:
            Rect windowBounds = Window.Current.Bounds;

            // Verify that the element is at least partially inside the window:
            if (elementBounds.X + elementBounds.Width < 0d ||
                elementBounds.Y + elementBounds.Height < 0d ||
                elementBounds.X > windowBounds.Width ||
                elementBounds.Y > windowBounds.Height)
            {
                return(false);
            }

            // Get the coordinates of the center point:
            Point centerPosition = new Point(elementBounds.X + elementBounds.Width / 2, elementBounds.Y + elementBounds.Height / 2);

            // Do a "HitTest" to verify that there is no element overlapping it:
            var elementAtCenterPosition = VisualTreeHelper.FindElementInHostCoordinates(centerPosition);

            if (elementAtCenterPosition == element)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#3
0
        /// <summary>
        /// This method returns null if no DragDropTarget is under the pointer, else it returns the DragDropTarget under it (the first Parent found)
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="itemContainerUnderPointer">The ListBoxItem or other container that is being moved. If there is no container, it is the item directly.</param>
        /// <returns>Null if no DragDropTarget is under the pointer, else it returns the DragDropTarget under it (the first Parent found)</returns>
        static DragDropTarget <TItemsControlType, TItemContainerType> GetDragDropTargetUnderPointer(double x, double y, out TItemContainerType itemContainerUnderPointer)
        {
            UIElement     element = VisualTreeHelper.FindElementInHostCoordinates(new Point(x, y));
            List <object> ElementsBetweenClickedElementAndDragDropTarget = new List <object>(); //This list will contain all the elements we go through when going from the clicked element to the DragDropTarget (both included)
            DragDropTarget <TItemsControlType, TItemContainerType> dragDropTargetUnder = null;

            // 1) Walk up the visual tree from the clicked element until we find the DragDropTarget:
            while (element != null)
            {
                ElementsBetweenClickedElementAndDragDropTarget.Add(element);
                if (element is DragDropTarget <TItemsControlType, TItemContainerType> )
                {
                    //------------------
                    // FOUND
                    //------------------
                    dragDropTargetUnder = (DragDropTarget <TItemsControlType, TItemContainerType>)element;
                    break;
                }

                //Move up to the parent
                element = (UIElement)element.INTERNAL_VisualParent;
            }

            if (dragDropTargetUnder != null)
            {
                //We found the DragDropTarget:
                // 2) Find the item to move from the list of the children of the DragDropTarget
                int indexOfLastElementInList = ElementsBetweenClickedElementAndDragDropTarget.Count - 1;
                int amoutOfElementsBetweenItemsRootAndDragDropTarget = dragDropTargetUnder.INTERNAL_GetNumberOfElementsBetweenItemsRootAndDragDropTarget();
                if (indexOfLastElementInList < amoutOfElementsBetweenItemsRootAndDragDropTarget) //Note: this can happen while dragging: the element under the pointer can be closer to the DragDropTarget than the root of the item we are dragging.
                {
                    itemContainerUnderPointer = null;
                    return(dragDropTargetUnder);
                }
                object elementToMove = ElementsBetweenClickedElementAndDragDropTarget.ElementAt(indexOfLastElementInList - amoutOfElementsBetweenItemsRootAndDragDropTarget);
                itemContainerUnderPointer = (TItemContainerType)elementToMove;
                return(dragDropTargetUnder);
            }
            else
            {
                // Not found:
                itemContainerUnderPointer = null;
                return(null);
            }
        }