2 using Xamarin.Forms.Platform;
4 namespace Xamarin.Forms
6 [RenderWith(typeof(_CheckBoxRenderer))]
7 public class CheckBox : View, IElementConfiguration<CheckBox>, IBorderElement, IColorElement
9 readonly Lazy<PlatformConfigurationRegistry<CheckBox>> _platformConfigurationRegistry;
10 public const string IsCheckedVisualState = "IsChecked";
12 public static readonly BindableProperty IsCheckedProperty =
13 BindableProperty.Create(nameof(IsChecked), typeof(bool), typeof(CheckBox), false,
14 propertyChanged: (bindable, oldValue, newValue) => {
15 ((CheckBox)bindable).CheckedChanged?.Invoke(bindable, new CheckedChangedEventArgs((bool)newValue));
16 ((CheckBox)bindable).ChangeVisualState();
17 }, defaultBindingMode: BindingMode.TwoWay);
19 public static readonly BindableProperty ColorProperty = ColorElement.ColorProperty;
23 get => (Color)GetValue(ColorProperty);
24 set => SetValue(ColorProperty, value);
28 public CheckBox() => _platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<CheckBox>>(() => new PlatformConfigurationRegistry<CheckBox>(this));
32 get => (bool)GetValue(IsCheckedProperty);
33 set => SetValue(IsCheckedProperty, value);
36 protected internal override void ChangeVisualState()
38 if (IsEnabled && IsChecked)
39 VisualStateManager.GoToState(this, IsCheckedVisualState);
41 base.ChangeVisualState();
44 public event EventHandler<CheckedChangedEventArgs> CheckedChanged;
46 public IPlatformElementConfiguration<T, CheckBox> On<T>() where T : IConfigPlatform
48 return _platformConfigurationRegistry.Value.On<T>();
51 void IBorderElement.OnBorderColorPropertyChanged(Color oldValue, Color newValue)
55 Color IBorderElement.BorderColor => Color.Transparent;
56 int IBorderElement.CornerRadius => 0;
57 double IBorderElement.BorderWidth => 0;
58 int IBorderElement.CornerRadiusDefaultValue => 0;
59 Color IBorderElement.BorderColorDefaultValue => Color.Transparent;
60 double IBorderElement.BorderWidthDefaultValue => 0;
61 bool IBorderElement.IsCornerRadiusSet() => false;
62 bool IBorderElement.IsBackgroundColorSet() => IsSet(BackgroundColorProperty);
63 bool IBorderElement.IsBorderColorSet() => false;
64 bool IBorderElement.IsBorderWidthSet() => false;