-
Notifications
You must be signed in to change notification settings - Fork 2
/
TouchableContextMenu (2).cs
87 lines (79 loc) · 2.78 KB
/
TouchableContextMenu (2).cs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace Start9.UI.Wpf
{
public class TouchableContextMenu : ContextMenu
{
public Boolean OpenedWithTouch
{
get => (Boolean)GetValue(OpenedWithTouchProperty);
set => SetValue(OpenedWithTouchProperty, value);
}
public static readonly DependencyProperty OpenedWithTouchProperty = DependencyProperty.Register("OpenedWithTouch",
typeof(Boolean), typeof(TouchableContextMenu),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
bool _wasOpenedWithTouch;
public TouchableContextMenu()
{
_wasOpenedWithTouch = false;
Loaded += TouchableContextMenu_Loaded;
Opened += (sneder, args) =>
{
Debug.WriteLine("_wasOpenedWithTouch: " + _wasOpenedWithTouch.ToString());
OpenedWithTouch = _wasOpenedWithTouch;
};
/*ContextMenuClosing += (sneder, args) =>
{
_wasOpenedWithTouch = false;
};*/
}
private void TouchableContextMenu_Loaded(Object sender, RoutedEventArgs e)
{
var source = ContextMenuService.GetPlacementTarget(this);
var placeTarget = PlacementTarget as UIElement;
if (source != null)
{
(source as UIElement).TouchDown += Source_TouchDown;
(source as UIElement).MouseDown += Source_MouseDown;
}
else if (placeTarget != null)
{
placeTarget.TouchDown += Source_TouchDown;
placeTarget.MouseDown += Source_MouseDown;
}
}
private void Source_MouseDown(Object sender, MouseButtonEventArgs e)
{
if (!((e.OriginalSource as UIElement).AreAnyTouchesOver))
_wasOpenedWithTouch = false;
}
private void Source_TouchDown(Object sender, TouchEventArgs e)
{
//TouchStarted = DateTime.Now;
Timer touchTimer = new Timer(1);
touchTimer.Elapsed += delegate
{
Dispatcher.Invoke(new Action(() =>
{
/*if (IsOpen)
{*/
_wasOpenedWithTouch = true;
/*}
else */if (!((e.OriginalSource as UIElement).AreAnyTouchesOver))
{
touchTimer.Stop();
}
}));
};
touchTimer.Start();
}
}
}