MobileCheckBox.xaml.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. public bool IsChecked
  33. {
  34. get => (bool)GetValue(IsCheckedProperty);
  35. set
  36. {
  37. SetValue(IsCheckedProperty, value);
  38. OnPropertyChanged(nameof(IsChecked));
  39. }
  40. }
  41. public event EventHandler Changed;
  42. public MobileCheckBox()
  43. {
  44. InitializeComponent();
  45. }
  46. private void ToggleCheck(object sender, EventArgs e)
  47. {
  48. IsChecked = !IsChecked;
  49. Changed?.Invoke(this,EventArgs.Empty);
  50. }
  51. }
  52. }