adae2c58c544fd2a9040cf327e6e9f3e8f0bb4d9
[platform/core/csapi/xsf.git] / src / XSF / Xamarin.Forms.Core / CheckBox.cs
1 using System;
2 using Xamarin.Forms.Platform;
3
4 namespace Xamarin.Forms
5 {
6         [RenderWith(typeof(_CheckBoxRenderer))]
7         public class CheckBox : View, IElementConfiguration<CheckBox>, IBorderElement, IColorElement
8         {
9                 readonly Lazy<PlatformConfigurationRegistry<CheckBox>> _platformConfigurationRegistry;
10                 public const string IsCheckedVisualState = "IsChecked";
11
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);
18
19                 public static readonly BindableProperty ColorProperty = ColorElement.ColorProperty;
20
21                 public Color Color
22                 {
23                         get => (Color)GetValue(ColorProperty);
24                         set => SetValue(ColorProperty, value);
25                 }
26
27
28                 public CheckBox() => _platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<CheckBox>>(() => new PlatformConfigurationRegistry<CheckBox>(this));
29
30                 public bool IsChecked
31                 {
32                         get => (bool)GetValue(IsCheckedProperty);
33                         set => SetValue(IsCheckedProperty, value);
34                 }
35
36                 protected internal override void ChangeVisualState()
37                 {
38                         if (IsEnabled && IsChecked)
39                                 VisualStateManager.GoToState(this, IsCheckedVisualState);
40                         else
41                                 base.ChangeVisualState();
42                 }
43
44                 public event EventHandler<CheckedChangedEventArgs> CheckedChanged;
45
46                 public IPlatformElementConfiguration<T, CheckBox> On<T>() where T : IConfigPlatform
47                 {
48                         return _platformConfigurationRegistry.Value.On<T>();
49                 }
50
51                 void IBorderElement.OnBorderColorPropertyChanged(Color oldValue, Color newValue)
52                 {
53                 }
54
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;
65         }
66 }