-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomCollectionView.xaml.cs
151 lines (127 loc) · 5.17 KB
/
CustomCollectionView.xaml.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.Collections;
namespace CustomCollectionView;
public partial class CustomCollectionView : ContentView
{
private VisualElement? _latestSelectedVisualElement;
public static readonly BindableProperty ItemTemplateProperty = BindableProperty.Create(nameof(ItemTemplate), typeof(DataTemplate), typeof(CustomCollectionView));
public DataTemplate ItemTemplate
{
get => (DataTemplate)GetValue(ItemTemplateProperty);
set => SetValue(ItemTemplateProperty, value);
}
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(nameof(ItemsSource), typeof(IEnumerable), typeof(CustomCollectionView));
public IEnumerable ItemsSource
{
get => (IEnumerable)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create(nameof(SelectedItem), typeof(object), typeof(CustomCollectionView), propertyChanged: OnSelectedItemPropertyChanged);
public object? SelectedItem
{
get => (object?)GetValue(SelectedItemProperty);
set => SetValue(SelectedItemProperty, value);
}
public static readonly BindableProperty SpacingProperty = BindableProperty.Create(nameof(Spacing), typeof(double), typeof(CustomCollectionView));
public double Spacing
{
get => (double)GetValue(SpacingProperty);
set => SetValue(SpacingProperty, value);
}
public event EventHandler<EventArgs>? SelectionChanged;
public CustomCollectionView()
{
InitializeComponent();
}
private void OnRootLayoutChildAdded(object sender, ElementEventArgs e)
{
if (e.Element is View view)
{
TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer()
{
NumberOfTapsRequired = 1,
};
PointerGestureRecognizer pointerGestureRecognizer = new PointerGestureRecognizer();
pointerGestureRecognizer.PointerEntered += OnPointerGestureRecognizePointerEntered;
pointerGestureRecognizer.PointerExited += OnPointerGestureRecognizePointerExited;
tapGestureRecognizer.Tapped += OnTapGestureRecognizerTapped;
view.GestureRecognizers.Add(tapGestureRecognizer);
view.GestureRecognizers.Add(pointerGestureRecognizer);
}
}
private void OnRootLayoutLoaded(object sender, EventArgs e)
{
if (SelectedItem != null)
{
foreach (var child in rootLayout.Children)
{
if (child is VisualElement visualElement)
{
if (visualElement.BindingContext == SelectedItem)
{
SetSelectedElement(visualElement);
}
else
{
VisualStateManager.GoToState(visualElement, "_Normal");
}
}
}
}
}
private void SetSelectedElement(VisualElement visualElement)
{
if (_latestSelectedVisualElement != null)
{
VisualStateManager.GoToState(_latestSelectedVisualElement, "_Normal");
}
_latestSelectedVisualElement = visualElement;
VisualStateManager.GoToState(visualElement, "_Selected");
SelectedItem = visualElement.BindingContext;
}
private void OnPointerGestureRecognizePointerExited(object? sender, PointerEventArgs e)
{
if (sender is VisualElement visualElement)
{
bool isSelected = ((VisualElement)sender).BindingContext == SelectedItem;
VisualStateManager.GoToState(visualElement, isSelected ? "_Selected" : "_Normal");
}
}
private void OnPointerGestureRecognizePointerEntered(object? sender, PointerEventArgs e)
{
if (sender is VisualElement visualElement)
{
bool isSelected = ((VisualElement)sender).BindingContext == SelectedItem;
if (!isSelected)
{
VisualStateManager.GoToState(visualElement, "_Hovered");
}
}
}
private void OnTapGestureRecognizerTapped(object? sender, TappedEventArgs e)
{
if (sender is VisualElement visualElement)
{
SetSelectedElement(visualElement);
}
}
private static void OnSelectedItemPropertyChanged(BindableObject bindable, object? oldValue, object? newValue)
{
var customCollectionView = (CustomCollectionView)bindable;
if (customCollectionView._latestSelectedVisualElement != null)
{
VisualStateManager.GoToState(customCollectionView._latestSelectedVisualElement, "_Normal");
}
if (newValue != null)
{
foreach (var child in customCollectionView.rootLayout.Children)
{
if (child is VisualElement childVisualElement && childVisualElement.BindingContext == newValue)
{
customCollectionView.SetSelectedElement(childVisualElement);
break;
}
}
}
customCollectionView.SelectionChanged?.Invoke(bindable, EventArgs.Empty);
}
}