Skip to content

Commit ec94a3a

Browse files
committed
#74 Modify ElementAdorner to not exceed the canvas itself when changing width and height
#74 Modify ElementAdorner to not exceed the canvas itself when changing width and height
1 parent 04a72dc commit ec94a3a

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

src/WPFDevelopers.Samples.Shared/Controls/ElementAdorner/ElementAdorner.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public double Angle
5656
private Vector startVector;
5757
public ElementAdorner(UIElement adornedElement) : base(adornedElement)
5858
{
59+
canvas = FindParent(adornedElement) as Canvas;
5960
visualCollection = new VisualCollection(this);
6061
visualCollection.Add(tMove = CreateMoveThumb());
6162
visualCollection.Add(tLeft = CreateThumb(Cursors.SizeNWSE, HorizontalAlignment.Left,
@@ -131,6 +132,13 @@ private Thumb CreateMoveThumb()
131132
return thumb;
132133
}
133134

135+
UIElement FindParent(UIElement element)
136+
{
137+
DependencyObject obj = element;
138+
obj = VisualTreeHelper.GetParent(obj);
139+
return obj as UIElement;
140+
}
141+
134142
private Brush GetMoveEllipse()
135143
{
136144
return new DrawingBrush(new GeometryDrawing(Brushes.Transparent, null, null));
@@ -166,13 +174,24 @@ private Thumb CreateThumb(Cursor cursor, HorizontalAlignment horizontal, Vertica
166174
switch (thumb.VerticalAlignment)
167175
{
168176
case VerticalAlignment.Bottom:
169-
if (element.Height + e.VerticalChange > ElementMiniSize) element.Height += e.VerticalChange;
177+
if (element.Height + e.VerticalChange > ElementMiniSize)
178+
{
179+
var newHeight = element.Height + e.VerticalChange;
180+
var top = Canvas.GetTop(element) + newHeight;
181+
if (newHeight > 0 && top <= canvas.ActualHeight)
182+
element.Height = newHeight;
183+
}
170184
break;
171185
case VerticalAlignment.Top:
172186
if (element.Height - e.VerticalChange > ElementMiniSize)
173187
{
174-
element.Height -= e.VerticalChange;
175-
Canvas.SetTop(element, Canvas.GetTop(element) + e.VerticalChange);
188+
var newHeight = element.Height - e.VerticalChange;
189+
var top = Canvas.GetTop(element) + e.VerticalChange;
190+
if (newHeight > 0 && top >= 0)
191+
{
192+
element.Height = newHeight;
193+
Canvas.SetTop(element, top);
194+
}
176195
}
177196

178197
break;
@@ -183,13 +202,24 @@ private Thumb CreateThumb(Cursor cursor, HorizontalAlignment horizontal, Vertica
183202
case HorizontalAlignment.Left:
184203
if (element.Width - e.HorizontalChange > ElementMiniSize)
185204
{
186-
element.Width -= e.HorizontalChange;
187-
Canvas.SetLeft(element, Canvas.GetLeft(element) + e.HorizontalChange);
205+
var newWidth = element.Width - e.HorizontalChange;
206+
var left = Canvas.GetLeft(element) + e.HorizontalChange;
207+
if (newWidth > 0 && left >= 0)
208+
{
209+
element.Width = newWidth;
210+
Canvas.SetLeft(element, left);
211+
}
188212
}
189213

190214
break;
191215
case HorizontalAlignment.Right:
192-
if (element.Width + e.HorizontalChange > ElementMiniSize) element.Width += e.HorizontalChange;
216+
if (element.Width + e.HorizontalChange > ElementMiniSize)
217+
{
218+
var newWidth = element.Width + e.HorizontalChange;
219+
var left = Canvas.GetLeft(element) + newWidth;
220+
if (newWidth > 0 && left <= canvas.ActualWidth)
221+
element.Width = newWidth;
222+
}
193223
break;
194224
}
195225

0 commit comments

Comments
 (0)