Skip to content

Commit

Permalink
update in ElementAdorner
Browse files Browse the repository at this point in the history
1) Fix the defect that the control disappears after the ElementAdorner control is rotated.
2) Fix ElementAdorner control dragging boundary.
  • Loading branch information
lifuhua123 committed Mar 8, 2023
1 parent cc34388 commit 33adcf1
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions src/WPFDevelopers.Shared/Controls/ElementAdorner/ElementAdorner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,24 @@ private Thumb CreateMoveThumb()
var element = AdornedElement as FrameworkElement;
if (element == null)
return;
Canvas.SetLeft(element, Canvas.GetLeft(element) + e.HorizontalChange);
Canvas.SetTop(element, Canvas.GetTop(element) + e.VerticalChange);

var transform = element.RenderTransform;
if (transform != null)
{
if (transform as RotateTransform != null)
{
var rotateTransform = transform as RotateTransform;

Point ptChange = rotateTransform.Transform(new Point(e.HorizontalChange, e.VerticalChange));

SetPositon(element, Canvas.GetLeft(element) + ptChange.X, Canvas.GetTop(element) + ptChange.Y);
}
else
{
SetPositon(element, Canvas.GetLeft(element) + e.HorizontalChange, Canvas.GetTop(element) + e.VerticalChange);
}
}

};
return thumb;
}
Expand Down Expand Up @@ -160,5 +176,32 @@ protected override Visual GetVisualChild(int index)
{
return visualCollection[index];
}

private void SetPositon(FrameworkElement element,double left, double top)
{
var parent = VisualTreeHelper.GetParent(element) as Canvas;

if (left <= 0)
{
left = 0;
}

if (top <= 0)
{
top = 0;
}

if (left + element.Width > parent.ActualWidth)
{
left = parent.ActualWidth - element.Width;
}
if (top + element.Height > parent.ActualHeight)
{
top = parent.ActualHeight - element.Height;
}

Canvas.SetLeft(element, left);
Canvas.SetTop(element, top);
}
}
}

0 comments on commit 33adcf1

Please sign in to comment.