MobileButtonStrip.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Collections.Specialized;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Xamarin.Forms;
  9. using Xamarin.Forms.Xaml;
  10. namespace InABox.Mobile
  11. {
  12. [XamlCompilation(XamlCompilationOptions.Compile)]
  13. public partial class MobileButtonStrip
  14. {
  15. /// <summary>
  16. /// The color of the Button Frames
  17. /// </summary>
  18. private readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  19. nameof(BorderColor),
  20. typeof(Color),
  21. typeof(MobileButtonStrip),
  22. Color.Gray);
  23. [TypeConverter(typeof(ColorTypeConverter))]
  24. public Color BorderColor
  25. {
  26. get => (Color)GetValue(BorderColorProperty);
  27. set => SetValue(BorderColorProperty, value);
  28. }
  29. private readonly BindableProperty UnselectedBackgroundColorProperty = BindableProperty.Create(
  30. nameof(UnselectedBackgroundColor),
  31. typeof(Color),
  32. typeof(MobileButtonStrip),
  33. XF.Material.Forms.Material.Color.SecondaryVariant);
  34. [TypeConverter(typeof(ColorTypeConverter))]
  35. public Color UnselectedBackgroundColor
  36. {
  37. get => (Color)GetValue(UnselectedBackgroundColorProperty);
  38. set => SetValue(UnselectedBackgroundColorProperty, value);
  39. }
  40. private readonly BindableProperty UnselectedForegroundColorProperty = BindableProperty.Create(
  41. nameof(UnselectedForegroundColor),
  42. typeof(Color),
  43. typeof(MobileButtonStrip),
  44. XF.Material.Forms.Material.Color.OnSecondary);
  45. [TypeConverter(typeof(ColorTypeConverter))]
  46. public Color UnselectedForegroundColor
  47. {
  48. get => (Color)GetValue(UnselectedForegroundColorProperty);
  49. set => SetValue(UnselectedForegroundColorProperty, value);
  50. }
  51. private readonly BindableProperty SelectedBackgroundColorProperty = BindableProperty.Create(
  52. nameof(SelectedBackgroundColor),
  53. typeof(Color),
  54. typeof(MobileButtonStrip),
  55. XF.Material.Forms.Material.Color.Surface);
  56. [TypeConverter(typeof(ColorTypeConverter))]
  57. public Color SelectedBackgroundColor
  58. {
  59. get => (Color)GetValue(SelectedBackgroundColorProperty);
  60. set => SetValue(SelectedBackgroundColorProperty, value);
  61. }
  62. private readonly BindableProperty SelectedForegroundColorProperty = BindableProperty.Create(
  63. nameof(SelectedForegroundColor),
  64. typeof(Color),
  65. typeof(MobileButtonStrip),
  66. XF.Material.Forms.Material.Color.OnSurface);
  67. [TypeConverter(typeof(ColorTypeConverter))]
  68. public Color SelectedForegroundColor
  69. {
  70. get => (Color)GetValue(SelectedForegroundColorProperty);
  71. set => SetValue(SelectedForegroundColorProperty, value);
  72. }
  73. private readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(
  74. nameof(CornerRadius),
  75. typeof(float),
  76. typeof(MobileButtonStrip),
  77. 5f);
  78. public float CornerRadius
  79. {
  80. get => (float)GetValue(CornerRadiusProperty);
  81. set => SetValue(CornerRadiusProperty, value);
  82. }
  83. public double ItemSpacing
  84. {
  85. get => _list.Spacing;
  86. set => _list.Spacing = value;
  87. }
  88. private readonly BindableProperty FontSizeProperty = BindableProperty.Create(
  89. nameof(FontSize),
  90. typeof(double),
  91. typeof(MobileButtonStrip),
  92. Device.GetNamedSize(NamedSize.Small, typeof(Label)));
  93. [TypeConverter(typeof(FontSizeConverter))]
  94. public double FontSize
  95. {
  96. get => (double)GetValue(FontSizeProperty);
  97. set => SetValue(FontSizeProperty, value);
  98. }
  99. private readonly BindableProperty FontAttributesProperty = BindableProperty.Create(
  100. nameof(FontAttributes),
  101. typeof(FontAttributes),
  102. typeof(MobileButtonStrip),
  103. FontAttributes.None);
  104. public FontAttributes FontAttributes
  105. {
  106. get => (FontAttributes)GetValue(FontAttributesProperty);
  107. set => SetValue(FontAttributesProperty, value);
  108. }
  109. public MobileButtonStripItem SelectedItem
  110. {
  111. get => Items.FirstOrDefault(x => x.Selected);
  112. set
  113. {
  114. foreach (var item in Items)
  115. item.Selected = item == value;
  116. }
  117. }
  118. public event EventHandler SelectionChanged;
  119. public MobileButtonStripItems Items { get; private set; }
  120. public MobileButtonStrip()
  121. {
  122. HeightRequest = 30;
  123. var items = new MobileButtonStripItems();
  124. items.CollectionChanged += ItemsChanged;
  125. Items = items;
  126. InitializeComponent();
  127. _list.BindingContext = Items;
  128. _list.SetValue(BindableLayout.ItemsSourceProperty, Items);
  129. //_list.ItemsSource = Items;
  130. }
  131. private void ItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
  132. {
  133. Device.BeginInvokeOnMainThread(() =>
  134. {
  135. SelectedItem ??= Items.FirstOrDefault();
  136. });
  137. }
  138. private void DoTap(object sender, EventArgs e)
  139. {
  140. Device.BeginInvokeOnMainThread(() =>
  141. {
  142. foreach (var item in Items)
  143. item.Selected = item == (sender as Label)?.BindingContext;
  144. OnPropertyChanged(nameof(Items));
  145. SelectionChanged?.Invoke(this,EventArgs.Empty);
  146. });
  147. }
  148. }
  149. }