Beispiel #1
0
        /// <summary>
        /// Applies a relative position translation to the associated element.
        /// </summary>
        /// <param name="x">The X component of the desired translation in root coordinates.</param>
        /// <param name="y">The Y component of the desired translation in root coordinates.</param>
        private void ApplyTranslation(double x, double y)
        {
            if (this.ParentElement == null)
            {
                this.ParentElement = this.AssociatedObject.Parent as FrameworkElement;
            }

            if (this.ParentElement == null)
            {
                return;
            }

            GeneralTransform rootToParent     = this.RootElement.TransformToVisual(this.ParentElement);
            Point            transformedPoint = MouseDragElementBehavior.TransformAsVector(rootToParent, x, y);

            x = transformedPoint.X;
            y = transformedPoint.Y;

            if (this.ConstrainToParentBounds)
            {
                FrameworkElement parentElement = this.ParentElement;
                Rect             parentBounds  = new Rect(0, 0, parentElement.ActualWidth, parentElement.ActualHeight);

                GeneralTransform objectToParent    = this.AssociatedObject.TransformToVisual(parentElement);
                Rect             objectBoundingBox = this.ElementBounds;
                objectBoundingBox = objectToParent.TransformBounds(objectBoundingBox);

                Rect endPosition = objectBoundingBox;
                endPosition.X += x;
                endPosition.Y += y;

                if (!MouseDragElementBehavior.RectContainsRect(parentBounds, endPosition))
                {
                    if (endPosition.X < parentBounds.Left)
                    {
                        double diff = endPosition.X - parentBounds.Left;
                        x -= diff;
                    }
                    else if (endPosition.Right > parentBounds.Right)
                    {
                        double diff = endPosition.Right - parentBounds.Right;
                        x -= diff;
                    }

                    if (endPosition.Y < parentBounds.Top)
                    {
                        double diff = endPosition.Y - parentBounds.Top;
                        y -= diff;
                    }
                    else if (endPosition.Bottom > parentBounds.Bottom)
                    {
                        double diff = endPosition.Bottom - parentBounds.Bottom;
                        y -= diff;
                    }
                }
            }

            this.ApplyTranslationTransform(x, y);
        }
Beispiel #2
0
        /// <summary>
        /// Updates the X and Y properties based on the current rendered position of the associated element.
        /// </summary>
        private void UpdatePosition()
        {
            GeneralTransform elementToRoot = this.AssociatedObject.TransformToVisual(this.RootElement);
            Point            translation   = MouseDragElementBehavior.GetTransformOffset(elementToRoot);

            this.X = translation.X;
            this.Y = translation.Y;
        }
Beispiel #3
0
 /// <summary>
 /// Attempts to update the position of the associated element to the specified coordinates.
 /// </summary>
 /// <param name="point">The desired position of the element in root coordinates.</param>
 private void UpdatePosition(Point point)
 {
     if (!this.settingPosition && this.AssociatedObject != null)
     {
         GeneralTransform elementToRoot = this.AssociatedObject.TransformToVisual(this.RootElement);
         Point            translation   = MouseDragElementBehavior.GetTransformOffset(elementToRoot);
         double           xChange       = double.IsNaN(point.X) ? 0 : point.X - translation.X;
         double           yChange       = double.IsNaN(point.Y) ? 0 : point.Y - translation.Y;
         this.ApplyTranslation(xChange, yChange);
     }
 }
Beispiel #4
0
        private static void OnConstrainToParentBoundsChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            MouseDragElementBehavior b = (MouseDragElementBehavior)sender;

            b.UpdatePosition(new Point(b.X, b.Y));
        }
Beispiel #5
0
        private static void OnYChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            MouseDragElementBehavior dragBehavior = (MouseDragElementBehavior)sender;

            dragBehavior.UpdatePosition(new Point(dragBehavior.X, (double)args.NewValue));
        }