This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 570
/
RadioBehavior.cs
145 lines (124 loc) · 4.69 KB
/
RadioBehavior.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
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Xamarin.FormsBook.Toolkit
{
public class RadioBehavior : Behavior<View>
{
TapGestureRecognizer tapRecognizer;
static List<RadioBehavior> defaultGroup = new List<RadioBehavior>();
static Dictionary<string, List<RadioBehavior>> radioGroups =
new Dictionary<string, List<RadioBehavior>>();
public RadioBehavior()
{
defaultGroup.Add(this);
}
public static readonly BindableProperty IsCheckedProperty =
BindableProperty.Create("IsChecked",
typeof(bool),
typeof(RadioBehavior),
false,
propertyChanged: OnIsCheckedChanged);
public bool IsChecked
{
set { SetValue(IsCheckedProperty, value); }
get { return (bool)GetValue(IsCheckedProperty); }
}
static void OnIsCheckedChanged(BindableObject bindable, object oldValue, object newValue)
{
RadioBehavior behavior = (RadioBehavior)bindable;
if ((bool)newValue)
{
string groupName = behavior.GroupName;
List<RadioBehavior> behaviors = null;
if (String.IsNullOrEmpty(groupName))
{
behaviors = defaultGroup;
}
else
{
behaviors = radioGroups[groupName];
}
foreach (RadioBehavior otherBehavior in behaviors)
{
if (otherBehavior != behavior)
{
otherBehavior.IsChecked = false;
}
}
}
}
public static readonly BindableProperty GroupNameProperty =
BindableProperty.Create("GroupName",
typeof(string),
typeof(RadioBehavior),
null,
propertyChanged: OnGroupNameChanged);
public string GroupName
{
set { SetValue(GroupNameProperty, value); }
get { return (string)GetValue(GroupNameProperty); }
}
static void OnGroupNameChanged(BindableObject bindable, object oldValue, object newValue)
{
RadioBehavior behavior = (RadioBehavior)bindable;
string oldGroupName = (string)oldValue;
string newGroupName = (string)newValue;
if (String.IsNullOrEmpty(oldGroupName))
{
// Remove the Behavior from the default group.
defaultGroup.Remove(behavior);
}
else
{
// Remove the RadioBehavior from the radioGroups collection.
List<RadioBehavior> behaviors = radioGroups[oldGroupName];
behaviors.Remove(behavior);
// Get rid of the collection if it's empty.
if (behaviors.Count == 0)
{
radioGroups.Remove(oldGroupName);
}
}
if (String.IsNullOrEmpty(newGroupName))
{
// Add the new Behavior to the default group.
defaultGroup.Add(behavior);
}
else
{
List<RadioBehavior> behaviors = null;
if (radioGroups.ContainsKey(newGroupName))
{
// Get the named group.
behaviors = radioGroups[newGroupName];
}
else
{
// If that group doesn't exist, create it.
behaviors = new List<RadioBehavior>();
radioGroups.Add(newGroupName, behaviors);
}
// Add the Behavior to the group.
behaviors.Add(behavior);
}
}
protected override void OnAttachedTo(View view)
{
base.OnAttachedTo(view);
tapRecognizer = new TapGestureRecognizer ();
tapRecognizer.Tapped += OnTapRecognizerTapped;
view.GestureRecognizers.Add(tapRecognizer);
}
protected override void OnDetachingFrom(View view)
{
base.OnDetachingFrom(view);
view.GestureRecognizers.Remove(tapRecognizer);
tapRecognizer.Tapped -= OnTapRecognizerTapped;
}
void OnTapRecognizerTapped(object sender, EventArgs args)
{
IsChecked = true;
}
}
}