protected void OnCalculateSize(ResizeArgs args)
        {
            var evnt = ResizeEvent;

            if (evnt != null)
            {
                evnt(args);
            }
        }
        // Handler for resizing from the top-left.
        void HandleTopLeft(object sender, DragDeltaEventArgs args)
        {
            FrameworkElement adornedElement = AdornedElement as FrameworkElement;
            Thumb            hitThumb       = sender as Thumb;

            if (adornedElement == null || hitThumb == null)
            {
                return;
            }

            // Ensure that the Width and Height are properly initialized after the resize.
            EnforceSize(adornedElement);

            double width_old = adornedElement.Width;
            double width_new = Math.Max(adornedElement.Width - args.HorizontalChange, hitThumb.DesiredSize.Width);
            double left_old  = Canvas.GetLeft(adornedElement);

            double height_old = adornedElement.Height;
            double height_new = Math.Max(adornedElement.Height - args.VerticalChange, hitThumb.DesiredSize.Height);
            double top_old    = Canvas.GetTop(adornedElement);

            var resizeArgs = new ResizeArgs
            {
                ResizeType = ResizeTypes.TopLeft,
                Element    = adornedElement,
                Position   = new Point(
                    (double)left_old - (width_new - width_old),
                    (double)top_old - (height_new - height_old)
                    ),
                Size = new Size(
                    (double)width_new,
                    (double)height_new
                    )
            };

            // Cheap static hook into resize
            OnCalculateSize(resizeArgs);

            if (resizeArgs.Cancel)
            {
                return;
            }

            adornedElement.Width = resizeArgs.Size.Width;
            Canvas.SetLeft(adornedElement, resizeArgs.Position.X);

            adornedElement.Height = resizeArgs.Size.Height;
            Canvas.SetTop(adornedElement, resizeArgs.Position.Y);
        }
        // Handler for resizing from the bottom-right.
        void HandleBottomRight(object sender, DragDeltaEventArgs args)
        {
            FrameworkElement adornedElement = this.AdornedElement as FrameworkElement;
            Thumb            hitThumb       = sender as Thumb;

            if (adornedElement == null || hitThumb == null)
            {
                return;
            }

            FrameworkElement parentElement = adornedElement.Parent as FrameworkElement;

            // Ensure that the Width and Height are properly initialized after the resize.
            EnforceSize(adornedElement);

            var resizeArgs = new ResizeArgs
            {
                ResizeType = ResizeTypes.BottomRight,
                Element    = adornedElement,
                Position   = new Point(
                    Canvas.GetLeft(adornedElement),
                    Canvas.GetTop(adornedElement)),
                Size = new Size(
                    (double)Math.Max(adornedElement.Width + args.HorizontalChange, hitThumb.DesiredSize.Width),
                    (double)Math.Max(args.VerticalChange + adornedElement.Height, hitThumb.DesiredSize.Height))
            };

            // Cheap static hook into resize
            OnCalculateSize(resizeArgs);

            if (resizeArgs.Cancel)
            {
                return;
            }

            // Change the size by the amount the user drags the mouse, as long as it's larger
            // than the width or height of an adorner, respectively.
            adornedElement.Width  = resizeArgs.Size.Width;
            adornedElement.Height = resizeArgs.Size.Height;
        }
        private void ResizingAdorner_ResizeEvent(ResizeArgs e)
        {
            Point referencePoint = Mouse.GetPosition(null);
            Point point          = Settings.Snap.Enabled ? Helper.SnapPoint(referencePoint) : referencePoint;

            var xDiff = referencePoint.X - point.X;
            var yDiff = referencePoint.Y - point.Y;

            var item = e.Element as ItemControl;

            switch (e.ResizeType)
            {
            case ResizeTypes.TopLeft:
            {
                #region Calc

                double newX = e.Position.X - xDiff;
                double newY = e.Position.Y - yDiff;

                double newWidth  = Math.Max(0, e.Size.Width + xDiff);
                double newHeight = Math.Max(0, e.Size.Height + yDiff);

                if (newWidth < item.MinWidth)
                {
                    newX    -= (item.MinWidth - newWidth);
                    newWidth = item.MinWidth;
                }

                if (newHeight < item.MinHeight)
                {
                    newY     -= (item.MinHeight - newHeight);
                    newHeight = item.MinHeight;
                }

                e.Position = new Point(newX, newY);
                e.Size     = new Size(newWidth, newHeight);

                UpdateSnapLine((SnapLinePositions.Top | SnapLinePositions.Left), e.Element);

                #endregion
            }
            break;

            case ResizeTypes.TopRight:
            {
                #region Calc

                double newY = e.Position.Y - yDiff;

                double newWidth  = Math.Max(0, e.Size.Width + xDiff);
                double newHeight = Math.Max(0, e.Size.Height + yDiff);

                if (newWidth < item.MinWidth)
                {
                    newWidth = item.MinWidth;
                }

                if (newHeight < item.MinHeight)
                {
                    newY     -= (item.MinHeight - newHeight);
                    newHeight = item.MinHeight;
                }

                e.Position = new Point(e.Position.X, newY);
                e.Size     = new Size(newWidth, newHeight);

                UpdateSnapLine((SnapLinePositions.Top | SnapLinePositions.Right), e.Element);

                #endregion
            }
            break;

            case ResizeTypes.BottomLeft:
            {
                #region Calc

                double newX = e.Position.X - xDiff;

                double newWidth  = Math.Max(0, e.Size.Width + xDiff);
                double newHeight = Math.Max(0, e.Size.Height - yDiff);

                if (newWidth < item.MinWidth)
                {
                    newX    -= (item.MinWidth - newWidth);
                    newWidth = item.MinWidth;
                }

                if (newHeight < item.MinHeight)
                {
                    newHeight = item.MinHeight;
                }

                e.Position = new Point(newX, e.Position.Y);
                e.Size     = new Size(newWidth, newHeight);

                UpdateSnapLine((SnapLinePositions.Bottom | SnapLinePositions.Left), e.Element);

                #endregion
            }
            break;

            case ResizeTypes.BottomRight:
            {
                #region Calc

                double newWidth  = Math.Max(0, e.Size.Width - xDiff);
                double newHeight = Math.Max(0, e.Size.Height - yDiff);

                if (newWidth < item.MinWidth)
                {
                    newWidth = item.MinWidth;
                }

                if (newHeight < item.MinHeight)
                {
                    newHeight = item.MinHeight;
                }

                e.Size = new Size(newWidth, newHeight);

                UpdateSnapLine((SnapLinePositions.Bottom | SnapLinePositions.Right), e.Element);

                #endregion
            }
            break;
            }

            ShowSnapLine();
            UpdateTitle();
        }