MobileCheckBox.xaml.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Globalization;
  3. using Xamarin.Forms;
  4. using Xamarin.Forms.Xaml;
  5. namespace InABox.Mobile
  6. {
  7. public class CheckBoxImageConverter : IValueConverter
  8. {
  9. public ImageSource Checked { get; set; }
  10. public ImageSource Unchecked { get; set; }
  11. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  12. {
  13. if ((value is bool ischecked) && ischecked)
  14. return Checked;
  15. return Unchecked;
  16. }
  17. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  18. {
  19. if (value is FileImageSource file)
  20. return String.Equals(file.File, "checked");
  21. return false;
  22. }
  23. }
  24. [XamlCompilation(XamlCompilationOptions.Compile)]
  25. public partial class MobileCheckBox
  26. {
  27. public static readonly BindableProperty IsCheckedProperty = BindableProperty.Create(
  28. nameof(IsChecked),
  29. typeof(bool),
  30. typeof(MobileCheckBox),
  31. false,
  32. BindingMode.TwoWay,
  33. propertyChanged: (sender,oldvalue,newvalue) => CheckChanged(sender as MobileCheckBox, (bool)oldvalue, (bool)newvalue));
  34. private static void CheckChanged(MobileCheckBox bindable, bool oldValue, bool newValue)
  35. {
  36. }
  37. public bool IsChecked
  38. {
  39. get => (bool)GetValue(IsCheckedProperty);
  40. set
  41. {
  42. SetValue(IsCheckedProperty, value);
  43. OnPropertyChanged(nameof(IsChecked));
  44. }
  45. }
  46. public event EventHandler Changed;
  47. public MobileCheckBox()
  48. {
  49. InitializeComponent();
  50. }
  51. private void ToggleCheck(object sender, EventArgs e)
  52. {
  53. IsChecked = !IsChecked;
  54. Changed?.Invoke(this,EventArgs.Empty);
  55. }
  56. }
  57. }