Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CompositeCheckboxGroup.cs
2using System.Collections.ObjectModel;
3using System.Windows.Input;
4using Microsoft.Maui.Controls;
5
7{
8 public class CompositeCheckboxGroup : StackLayout
9 {
10 public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
11 nameof(ItemsSource), typeof(IEnumerable), typeof(CompositeCheckboxGroup), propertyChanged: OnItemsSourceChanged);
12
13 public static readonly BindableProperty SelectedItemsProperty = BindableProperty.Create(
14 nameof(SelectedItems), typeof(IList), typeof(CompositeCheckboxGroup), defaultValue: new ObservableCollection<object>(),
15 defaultBindingMode: BindingMode.TwoWay);
16
17 public static readonly BindableProperty LabelTextProperty = BindableProperty.Create(
18 nameof(LabelText), typeof(string), typeof(CompositeCheckboxGroup), default(string));
19
20 public static readonly BindableProperty RequiredProperty = BindableProperty.Create(
21 nameof(Required), typeof(bool), typeof(CompositeCheckboxGroup), default(bool));
22
23 public static readonly BindableProperty IsValidProperty = BindableProperty.Create(
24 nameof(IsValid), typeof(bool), typeof(CompositeCheckboxGroup), default(bool));
25
26 public static readonly BindableProperty ValidationTextProperty = BindableProperty.Create(
27 nameof(ValidationText), typeof(string), typeof(CompositeCheckboxGroup), default(string));
28
29 public IEnumerable ItemsSource
30 {
31 get => (IEnumerable)this.GetValue(ItemsSourceProperty);
32 set => this.SetValue(ItemsSourceProperty, value);
33 }
34
35 public IList SelectedItems
36 {
37 get => (IList)this.GetValue(SelectedItemsProperty);
38 set => this.SetValue(SelectedItemsProperty, value);
39 }
40
41 public string LabelText
42 {
43 get => (string)this.GetValue(LabelTextProperty);
44 set => this.SetValue(LabelTextProperty, value);
45 }
46
47 public bool Required
48 {
49 get => (bool)this.GetValue(RequiredProperty);
50 set => this.SetValue(RequiredProperty, value);
51 }
52
53 public bool IsValid
54 {
55 get => (bool)this.GetValue(IsValidProperty);
56 set => this.SetValue(IsValidProperty, value);
57 }
58
59 public string ValidationText
60 {
61 get => (string)this.GetValue(ValidationTextProperty);
62 set => this.SetValue(ValidationTextProperty, value);
63 }
64
66 {
67 this.Orientation = StackOrientation.Vertical;
68 }
69
70 private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
71 {
72 if (bindable is CompositeCheckboxGroup Group)
73 {
74 Group.BuildCheckboxes();
75 }
76 }
77
78 private void BuildCheckboxes()
79 {
80 this.Children.Clear();
81 if (!string.IsNullOrEmpty(this.LabelText))
82 {
83 Label HeaderLabel = new Label
84 {
85 Text = this.LabelText,
86 FontAttributes = FontAttributes.Bold
87 };
88 this.Children.Add(HeaderLabel);
89 }
90 if (this.ItemsSource == null)
91 {
92 return;
93 }
94 foreach (object Item in this.ItemsSource)
95 {
96 CheckBox CheckBox = new CheckBox
97 {
98 BindingContext = Item,
99 IsChecked = this.SelectedItems?.Contains(Item) == true
100 };
101 CheckBox.CheckedChanged += (object? sender, CheckedChangedEventArgs e) =>
102 {
103 if (this.SelectedItems == null)
104 {
105 return;
106 }
107 if (e.Value)
108 {
109 if (!this.SelectedItems.Contains(Item))
110 {
111 this.SelectedItems.Add(Item);
112 }
113 }
114 else
115 {
116 if (this.SelectedItems.Contains(Item))
117 {
118 this.SelectedItems.Remove(Item);
119 }
120 }
121 };
122
123 Label OptionLabel = new Label
124 {
125 VerticalTextAlignment = TextAlignment.Center
126 };
127 OptionLabel.SetBinding(Label.TextProperty, "Label.Text");
128
129 HorizontalStackLayout Row = new HorizontalStackLayout
130 {
131 Spacing = 8
132 };
133 Row.Children.Add(CheckBox);
134 Row.Children.Add(OptionLabel);
135 this.Children.Add(Row);
136 }
137 if (!this.IsValid && !string.IsNullOrEmpty(this.ValidationText))
138 {
139 Label ValidationLabel = new Label
140 {
141 Text = this.ValidationText,
142 TextColor = Colors.Red,
143 FontSize = 12
144 };
145 this.Children.Add(ValidationLabel);
146 }
147 }
148 }
149}