-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathDragBehavior.cs
More file actions
54 lines (44 loc) · 1.45 KB
/
DragBehavior.cs
File metadata and controls
54 lines (44 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Microsoft.Xaml.Interactivity;
using Windows.Foundation;
namespace BehaviorSample;
public class DragBehavior : Behavior<FrameworkElement>
{
private TranslateTransform _transform = new();
private FrameworkElement? _parent;
private Point _prevPoint;
private long _pointerId;
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.RenderTransform = _transform;
AssociatedObject.PointerPressed += (sender, e) =>
{
if (AssociatedObject.Parent is FrameworkElement parent)
{
_parent = parent;
_prevPoint = e.GetCurrentPoint(_parent).Position;
_parent.PointerMoved += (sender, e) =>
{
if (e.Pointer.PointerId != _pointerId) return;
var pos = e.GetCurrentPoint(_parent).Position;
_transform.X += pos.X - _prevPoint.X;
_transform.Y += pos.Y - _prevPoint.Y;
_prevPoint = pos;
};
_pointerId = e.Pointer.PointerId;
}
};
AssociatedObject.PointerReleased += (sender, e) =>
{
if (e.Pointer.PointerId != _pointerId) return;
_pointerId = -1;
};
}
protected override void OnDetaching()
{
_parent = null;
_pointerId = -1;
}
}