-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSegmentView.xaml.cs
218 lines (174 loc) · 6.71 KB
/
SegmentView.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System.Diagnostics;
using LantzControls.Input.Models;
using Telerik.Maui.Controls;
namespace LantzControls.Input;
public partial class SegmentView : ContentView
{
public SegmentView()
{
InitializeComponent();
}
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create("ItemsSource", typeof(IList<SegmentItem>), typeof(SegmentView), null, propertyChanged: OnItemsSourceChanged);
public static readonly BindableProperty SelectedIndexProperty =
BindableProperty.Create("SelectedIndex", typeof(int), typeof(SegmentView), null, propertyChanged: OnSelectedIndexChanged);
public static readonly BindableProperty CornerRadiusProperty =
BindableProperty.Create("CornerRadius", typeof(int), typeof(SegmentView), null, propertyChanged: OnCornerRadiusChanged);
public static readonly BindableProperty FontSizeProperty =
BindableProperty.Create("FontSize", typeof(int), typeof(SegmentView), null);
public static readonly BindableProperty SelectedSegmentBackgroundColorProperty =
BindableProperty.Create("SelectedSegmentBackgroundColor", typeof(Color), typeof(SegmentView));
public static readonly BindableProperty SelectedSegmentTextColorProperty =
BindableProperty.Create("SelectedSegmentTextColor", typeof(Color), typeof(SegmentView));
public static readonly BindableProperty BorderColorProperty =
BindableProperty.Create("BorderColor", typeof(Color), typeof(SegmentView), null, propertyChanged: OnBorderColorChanged);
public IList<SegmentItem> ItemsSource
{
get => (IList<SegmentItem>)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
public int SelectedIndex
{
get => (int)GetValue(SelectedIndexProperty);
set => SetValue(SelectedIndexProperty, value);
}
public int CornerRadius
{
get => (int)GetValue(CornerRadiusProperty);
set => SetValue(CornerRadiusProperty, value);
}
public int FontSize
{
get => (int)GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
public Color SelectedSegmentBackgroundColor
{
get => (Color)GetValue(SelectedSegmentBackgroundColorProperty);
set => SetValue(SelectedSegmentBackgroundColorProperty, value);
}
public Color SelectedSegmentTextColor
{
get => (Color)GetValue(SelectedSegmentTextColorProperty);
set => SetValue(SelectedSegmentTextColorProperty, value);
}
public Color BorderColor
{
get => (Color)GetValue(BorderColorProperty);
set => SetValue(BorderColorProperty, value);
}
// Bindable PropertyChanged Handlers
private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is SegmentView self && newValue is IList<SegmentItem>)
{
self.CreateButtons();
}
}
private static void OnSelectedIndexChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is SegmentView self)
{
if ((int)newValue > self.ItemsSource.Count - 1)
{
throw new ArgumentOutOfRangeException($"The SelectedIndex {(int)newValue} is out of range");
}
self.SetSelectedButtonColor();
self.SelectedItemChanged?.Invoke(self, new SelectedItemChangedEventArgs(self.ItemsSource[(int)newValue], (int)newValue));
}
}
private static void OnCornerRadiusChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is SegmentView self)
{
self.RootBorder.CornerRadius = (int)newValue;
}
}
private static void OnBorderColorChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is SegmentView self)
{
self.RootBorder.BorderColor = (Color)newValue;
}
}
public event EventHandler<SelectedItemChangedEventArgs> SelectedItemChanged;
#region Internal Methods
private void CreateButtons()
{
// ensuring no abandoned event handlers
if (ButtonsGrid.Children.Any())
{
foreach (var child in ButtonsGrid.Children)
{
if (child is RadButton childButton)
{
childButton.Clicked -= Button_Clicked;
}
}
}
// remove previous buttons
ButtonsGrid.Children.Clear();
if (ItemsSource == null)
return;
// Create new buttons
foreach (var item in ItemsSource)
{
var index = ItemsSource.IndexOf(item);
ButtonsGrid.ColumnDefinitions.Add(new ColumnDefinition());
var button = new RadButton();
// Pass down the properties to the RadButton
button.Text = item.Text;
button.ImageSource = item.ImageSource;
button.BackgroundImage = item.BackgroundImageSource;
// From BindableProperties
button.FontSize = this.FontSize;
// do not customize these, they're used
button.BorderThickness = 0;
button.BorderColor = Colors.Transparent;
Grid.SetColumn(button, index);
// Important: this adds the "walls" between segments
if (index > 0)
{
button.BorderThickness = new Thickness(2, 0, 0, 0);
button.BorderColor = BorderColor;
}
button.Clicked += Button_Clicked;
ButtonsGrid.Children.Add(button);
}
SetSelectedButtonColor();
}
// Changes the SelectedIndex
private void Button_Clicked(object sender, System.EventArgs e)
{
if (sender is not RadButton clickedButton)
return;
try
{
this.SelectedIndex = ButtonsGrid.Children.IndexOf(clickedButton);
}
catch (Exception ex)
{
Debug.WriteLine($"SegmentView Exception: {ex.Message}");
}
}
// This will change the segment color according to the selected index.
private void SetSelectedButtonColor()
{
foreach (var child in ButtonsGrid.Children)
{
if (child is not RadButton childButton)
continue;
if (childButton.Text == ItemsSource[SelectedIndex].Text)
{
childButton.BackgroundColor = SelectedSegmentBackgroundColor;
childButton.TextColor = SelectedSegmentTextColor;
}
else
{
childButton.BackgroundColor = Colors.White;
childButton.TextColor = Colors.Black;
}
}
}
#endregion
}