-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ListBoxAssist.cs
63 lines (51 loc) · 2.29 KB
/
ListBoxAssist.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
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace MaterialDesignThemes.Wpf
{
public static class ListBoxAssist
{
static ListBoxAssist()
{
EventManager.RegisterClassHandler(typeof (ListBox), UIElement.PreviewMouseLeftButtonDownEvent,
new MouseButtonEventHandler(ListBoxMouseButtonEvent));
}
private static void ListBoxMouseButtonEvent(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
var senderElement = (UIElement) sender;
if (!GetIsToggle(senderElement)) return;
var point = mouseButtonEventArgs.GetPosition(senderElement);
var result = VisualTreeHelper.HitTest(senderElement, point);
if (result == null) return;
ListBoxItem listBoxItem = null;
Ripple ripple = null;
foreach (var dependencyObject in result.VisualHit.GetVisualAncestry().TakeWhile(_ => listBoxItem == null))
{
listBoxItem = dependencyObject as ListBoxItem;
if (ripple == null)
ripple = dependencyObject as Ripple;
}
if (listBoxItem == null || !listBoxItem.IsEnabled) return;
listBoxItem.SetCurrentValue(ListBoxItem.IsSelectedProperty, !listBoxItem.IsSelected);
mouseButtonEventArgs.Handled = true;
if (ripple != null && listBoxItem.IsSelected)
{
ripple.RaiseEvent(new MouseButtonEventArgs(mouseButtonEventArgs.MouseDevice, mouseButtonEventArgs.Timestamp, mouseButtonEventArgs.ChangedButton)
{ RoutedEvent = Control.PreviewMouseLeftButtonDownEvent, Source = ripple }
);
}
}
public static readonly DependencyProperty IsToggleProperty = DependencyProperty.RegisterAttached(
"IsToggle", typeof(bool), typeof(ListBoxAssist), new FrameworkPropertyMetadata(default(bool)));
public static void SetIsToggle(DependencyObject element, bool value)
{
element.SetValue(IsToggleProperty, value);
}
public static bool GetIsToggle(DependencyObject element)
{
return (bool)element.GetValue(IsToggleProperty);
}
}
}