MobileRadioList.xaml.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using Xamarin.Forms;
  6. using Xamarin.Forms.Xaml;
  7. namespace InABox.Mobile
  8. {
  9. public class MobileRadioListChangedEventArgs : EventArgs
  10. {
  11. }
  12. public delegate void MobileRadioListChangedEvent(object sender, MobileRadioListChangedEventArgs args);
  13. [XamlCompilation(XamlCompilationOptions.Compile)]
  14. public partial class MobileRadioList
  15. {
  16. private readonly BindableProperty BackgroundColorProperty = BindableProperty.Create(
  17. nameof(BackgroundColor),
  18. typeof(Color),
  19. typeof(MobileRadioList),
  20. XF.Material.Forms.Material.Color.Surface);
  21. [TypeConverter(typeof(ColorTypeConverter))]
  22. public Color BackgroundColor
  23. {
  24. get => (Color)GetValue(BackgroundColorProperty);
  25. set => SetValue(BackgroundColorProperty, value);
  26. }
  27. private readonly BindableProperty TextColorProperty = BindableProperty.Create(
  28. nameof(TextColor),
  29. typeof(Color),
  30. typeof(MobileRadioList),
  31. XF.Material.Forms.Material.Color.OnSurface);
  32. [TypeConverter(typeof(ColorTypeConverter))]
  33. public Color TextColor
  34. {
  35. get => (Color)GetValue(TextColorProperty);
  36. set => SetValue(TextColorProperty, value);
  37. }
  38. private readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  39. nameof(BorderColor),
  40. typeof(Color),
  41. typeof(MobileRadioList),
  42. XF.Material.Forms.Material.Color.Secondary);
  43. public Color BorderColor
  44. {
  45. get => (Color)GetValue(BorderColorProperty);
  46. set => SetValue(BorderColorProperty, value);
  47. }
  48. public IList<MobileRadioListItem> Items { get; }
  49. public MobileRadioListItem SelectedItem
  50. {
  51. get => Items.FirstOrDefault(x => x.IsChecked);
  52. set
  53. {
  54. foreach (var item in Items)
  55. item.IsChecked = item == value;
  56. }
  57. }
  58. public event MobileRadioListChangedEvent Changed;
  59. public MobileRadioList()
  60. {
  61. Items = new ObservableCollection<MobileRadioListItem>();
  62. InitializeComponent();
  63. BindingContext = this;
  64. _list.ItemsSource = Items;
  65. BackgroundColor = XF.Material.Forms.Material.Color.Surface;
  66. }
  67. private void Tapped(object sender, EventArgs e)
  68. {
  69. if ((sender as BindableObject)?.BindingContext is MobileRadioListItem item)
  70. {
  71. if (item != SelectedItem)
  72. {
  73. SelectedItem = item;
  74. Changed?.Invoke(this, new MobileRadioListChangedEventArgs());
  75. _list.ItemsSource = null;
  76. _list.ItemsSource = Items;
  77. }
  78. }
  79. }
  80. }
  81. }