-
Notifications
You must be signed in to change notification settings - Fork 2
/
TouchableContextMenu.cs
72 lines (66 loc) · 2.26 KB
/
TouchableContextMenu.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
using System;
using System.Collections.Generic;
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));
public TouchableContextMenu()
{
Loaded += TouchableContextMenu_Loaded;
}
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)
{
OpenedWithTouch = 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)
{
OpenedWithTouch = true;
}
else if (!((e.OriginalSource as UIElement).AreAnyTouchesOver))
{
touchTimer.Stop();
}
}));
};
touchTimer.Start();
}
}
}