MultiButtonUserControl.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Xamarin.Forms;
  8. using Xamarin.Forms.Xaml;
  9. namespace PRS.Mobile
  10. {
  11. [XamlCompilation(XamlCompilationOptions.Compile)]
  12. //custom 3 button class:
  13. //note: 1.) ButtonSelected is a string that controls which button is green ("#15C7C1") - button1, 2 and 3 .Text properties must be set for this to work properly (set in xaml)
  14. // 2.) always use SetFirstButton() in the OnAppearing() method along with the LoadData() method for that screen - this allows the screen to default to the right button and list selected
  15. public partial class MultiButtonUserControl : Grid, INotifyPropertyChanged
  16. {
  17. Color color = Color.FromHex("#15C7C1");
  18. public event System.EventHandler ButtonChangedEvent;
  19. public String ButtonSelected
  20. {
  21. get; set;
  22. }
  23. public MultiButtonUserControl()
  24. {
  25. InitializeComponent();
  26. Button1.BackgroundColor = color;
  27. }
  28. public void SetFirstButton()
  29. {
  30. Button1.BackgroundColor = color; Button2.BackgroundColor = default; Button3.BackgroundColor = default;
  31. }
  32. protected virtual void OnButtonChangedEvent()
  33. {
  34. if (ButtonChangedEvent != null) ButtonChangedEvent(this, EventArgs.Empty);
  35. }
  36. //remember that ChangeColours also triggers OnButtonChangedEvent
  37. private void ChangeColours()
  38. {
  39. if (ButtonSelected.Equals(Button1Text))
  40. {
  41. Button1.BackgroundColor = color; Button2.BackgroundColor = default; Button3.BackgroundColor = default;
  42. }
  43. if (ButtonSelected.Equals(Button2Text))
  44. {
  45. Button2.BackgroundColor = color; Button1.BackgroundColor = default; Button3.BackgroundColor = default;
  46. }
  47. if (ButtonSelected.Equals(Button3Text))
  48. {
  49. Button3.BackgroundColor = color; Button1.BackgroundColor = default; Button2.BackgroundColor = default;
  50. }
  51. OnButtonChangedEvent();
  52. }
  53. #region button text properties
  54. public string Button1Text
  55. {
  56. get { return Button1.Text; }
  57. set { Button1.Text = value; }
  58. }
  59. public string Button2Text
  60. {
  61. get { return Button2.Text; }
  62. set { Button2.Text = value; }
  63. }
  64. public string Button3Text
  65. {
  66. get { return Button3.Text; }
  67. set { Button3.Text = value; }
  68. }
  69. #endregion
  70. #region button clicked event handlers
  71. public void Button1_Clicked(object sender, EventArgs e)
  72. {
  73. ButtonSelected = Button1Text;
  74. ChangeColours();
  75. }
  76. public void Button2_Clicked(object sender, EventArgs e)
  77. {
  78. ButtonSelected = Button2Text;
  79. ChangeColours();
  80. }
  81. public void Button3_Clicked(object sender, EventArgs e)
  82. {
  83. ButtonSelected = Button3Text;
  84. ChangeColours();
  85. }
  86. #endregion
  87. //this can attach bindable properties
  88. //public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(MultiButtonUserControl));
  89. // public string Text
  90. //{
  91. // get { return (string)GetValue(TextProperty); }
  92. // set { SetValue(TextProperty, value); }
  93. //}
  94. }
  95. }