예제 #1
0
        public static UIElement GetClosestItemContainerToPosition(ItemsControl itemsControl, Point point, Orientation orientation)
        {
            Type itemContainerType = ControlMethods.GetItemContainerType(itemsControl);

            if (itemContainerType == null)
            {
                return(null);
            }
            List <UIElement> containers = new List <UIElement>();
            LineGeometry     line;

            switch (orientation)
            {
            case Orientation.Horizontal:
                line = new LineGeometry(new Point(0d, point.Y), new Point(itemsControl.RenderSize.Width, point.Y));
                break;

            default:
                line = new LineGeometry(new Point(point.X, 0d), new Point(point.X, itemsControl.RenderSize.Height));
                break;
            }
            line.Freeze();
            VisualTreeHelper.HitTest(
                itemsControl,
                null,
                testResult =>
            {
                UIElement itemContainer = testResult.VisualHit.FindVisualTreeAncestor(itemContainerType) as UIElement;
                if (itemContainer != null && itemsControl.Equals(ItemsControl.ItemsControlFromItemContainer(itemContainer)))
                {
                    containers.Add(itemContainer);
                }
                return(HitTestResultBehavior.Continue);
            },
                new GeometryHitTestParameters(line)
                );
            switch (containers.Count)
            {
            case 0:
                return(null);

            case 1:
                return(containers[0]);
            }
            // Find closest item to the point clicked.
            UIElement closest         = null;
            double    closestDistance = double.MaxValue;

            foreach (UIElement container in containers)
            {
                Point  transform = container.TransformToAncestor(itemsControl).Transform(default(Point));
                double distance  = Math.Abs(orientation == Orientation.Horizontal ? point.X - transform.X : point.Y - transform.Y);
                if (distance < closestDistance)
                {
                    closest         = container;
                    closestDistance = distance;
                }
            }
            return(closest);
        }
예제 #2
0
        public static UIElement GetItemContainer(this ItemsControl itemsControl, UIElement child)
        {
            Type itemContainerType = ControlMethods.GetItemContainerType(itemsControl);

            if (itemContainerType == null)
            {
                return(null);
            }
            for (UIElement element = VisualTreeHelper.GetParent(child) as UIElement; element != null && !itemsControl.Equals(element); element = VisualTreeHelper.GetParent(element) as UIElement)
            {
                if (itemContainerType.IsInstanceOfType(element) && itemsControl.Equals(ItemsControl.ItemsControlFromItemContainer(element)))
                {
                    return(element);
                }
            }
            return(null);
        }