2using System.Collections.ObjectModel;
3using System.Windows.Input;
4using Microsoft.Maui.Controls;
10 public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
11 nameof(ItemsSource), typeof(IEnumerable), typeof(
CompositeCheckboxGroup), propertyChanged: OnItemsSourceChanged);
13 public static readonly BindableProperty SelectedItemsProperty = BindableProperty.Create(
14 nameof(SelectedItems), typeof(IList), typeof(
CompositeCheckboxGroup), defaultValue:
new ObservableCollection<object>(),
15 defaultBindingMode: BindingMode.TwoWay);
17 public static readonly BindableProperty LabelTextProperty = BindableProperty.Create(
20 public static readonly BindableProperty RequiredProperty = BindableProperty.Create(
23 public static readonly BindableProperty IsValidProperty = BindableProperty.Create(
26 public static readonly BindableProperty ValidationTextProperty = BindableProperty.Create(
29 public IEnumerable ItemsSource
31 get => (IEnumerable)this.GetValue(ItemsSourceProperty);
32 set => this.SetValue(ItemsSourceProperty, value);
35 public IList SelectedItems
37 get => (IList)this.GetValue(SelectedItemsProperty);
38 set => this.SetValue(SelectedItemsProperty, value);
41 public string LabelText
43 get => (string)this.GetValue(LabelTextProperty);
44 set => this.SetValue(LabelTextProperty, value);
49 get => (bool)this.GetValue(RequiredProperty);
50 set => this.SetValue(RequiredProperty, value);
55 get => (bool)this.GetValue(IsValidProperty);
56 set => this.SetValue(IsValidProperty, value);
59 public string ValidationText
61 get => (string)this.GetValue(ValidationTextProperty);
62 set => this.SetValue(ValidationTextProperty, value);
67 this.Orientation = StackOrientation.Vertical;
70 private static void OnItemsSourceChanged(BindableObject bindable,
object oldValue,
object newValue)
74 Group.BuildCheckboxes();
78 private void BuildCheckboxes()
80 this.Children.Clear();
81 if (!
string.IsNullOrEmpty(this.LabelText))
83 Label HeaderLabel =
new Label
85 Text = this.LabelText,
86 FontAttributes = FontAttributes.Bold
88 this.Children.Add(HeaderLabel);
90 if (this.ItemsSource ==
null)
94 foreach (
object Item
in this.ItemsSource)
96 CheckBox CheckBox =
new CheckBox
98 BindingContext = Item,
99 IsChecked = this.SelectedItems?.Contains(Item) ==
true
101 CheckBox.CheckedChanged += (
object? sender, CheckedChangedEventArgs e) =>
103 if (this.SelectedItems ==
null)
109 if (!this.SelectedItems.Contains(Item))
111 this.SelectedItems.Add(Item);
116 if (this.SelectedItems.Contains(Item))
118 this.SelectedItems.Remove(Item);
123 Label OptionLabel =
new Label
125 VerticalTextAlignment = TextAlignment.Center
127 OptionLabel.SetBinding(Label.TextProperty,
"Label.Text");
129 HorizontalStackLayout Row =
new HorizontalStackLayout
133 Row.Children.Add(CheckBox);
134 Row.Children.Add(OptionLabel);
135 this.Children.Add(Row);
137 if (!this.IsValid && !
string.IsNullOrEmpty(this.ValidationText))
139 Label ValidationLabel =
new Label
141 Text = this.ValidationText,
142 TextColor = Colors.Red,
145 this.Children.Add(ValidationLabel);