-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRFilterItem.cs
130 lines (112 loc) · 4.14 KB
/
RFilterItem.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Input;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
namespace Ratio.UWP.Controls
{
[TemplateVisualState(GroupName = "ItemsStates", Name = "Normal")]
[TemplateVisualState(GroupName = "ItemsStates", Name = "PointerOver")]
[TemplateVisualState(GroupName = "ItemsStates", Name = "SelectedState")]
public sealed class RFilterItem : ContentControl
{
public event EventHandler<object> ItemSelected;
#region Fields
private string _pendingState;
#endregion
#region Properties
public static readonly DependencyProperty SelectedProperty = DependencyProperty.Register(
"Selected", typeof(bool), typeof(RFilterItem), new PropertyMetadata(default(bool), SelectedChanged));
private static void SelectedChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var rFilterItem = dependencyObject as RFilterItem;
if (rFilterItem == null) return;
if ((bool)dependencyPropertyChangedEventArgs.NewValue)
{
var success = VisualStateManager.GoToState(rFilterItem, "SelectedState", true);
if (!success) rFilterItem._pendingState = "SelectedState";
}
else
{
var success = VisualStateManager.GoToState(rFilterItem, "Normal", true);
if (!success) rFilterItem._pendingState = "Normal";
}
}
public bool Selected
{
get => (bool)GetValue(SelectedProperty);
set => SetValue(SelectedProperty, value);
}
public List<RFilterItem> Siblings { get; set; }
#endregion
public RFilterItem()
{
DefaultStyleKey = typeof(RFilterItem);
Siblings = new List<RFilterItem>();
Loaded += (sender, args) =>
{
VisualStateManager.GoToState(this, "Normal", true);
LayoutUpdated += OnLayoutUpdated;
};
}
private void OnLayoutUpdated(object o, object o1)
{
if (_pendingState != null) VisualStateManager.GoToState(this, _pendingState, true);
LayoutUpdated -= OnLayoutUpdated;
}
protected override void OnPointerEntered(PointerRoutedEventArgs e)
{
var success = VisualStateManager.GoToState(this, "PointerOver", true);
Debug.WriteLine($"State change to PointerOver success: {success}");
}
protected override void OnTapped(TappedRoutedEventArgs e)
{
if (Selected) return;
SetSelected();
OnItemSelected(DataContext);
}
protected override void OnKeyUp(KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Space || e.Key == VirtualKey.Enter)
{
SetSelected();
OnItemSelected(DataContext);
}
base.OnKeyUp(e);
}
private void SetSelected()
{
if (Selected) return;
Selected = true;
foreach (var rFilterItem in Siblings)
{
rFilterItem.Selected = false;
}
}
protected override void OnPointerExited(PointerRoutedEventArgs e)
{
if (Selected)
{
var success = VisualStateManager.GoToState(this, "SelectedState", true);
Debug.WriteLine($"State change to SelectedState success: {success}");
}
else
{
var success = VisualStateManager.GoToState(this, "Normal", true);
Debug.WriteLine($"State change to Normal success: {success}");
}
}
protected override void OnGotFocus(RoutedEventArgs e)
{
Debug.WriteLine("Filter Item got focus.");
base.OnGotFocus(e);
}
private void OnItemSelected(object e)
{
ItemSelected?.Invoke(this, e);
}
}
}