using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace PRS.Mobile { [XamlCompilation(XamlCompilationOptions.Compile)] //custom 3 button class: //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) // 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 public partial class MultiButtonUserControl : Grid, INotifyPropertyChanged { Color color = Color.FromHex("#15C7C1"); public event System.EventHandler ButtonChangedEvent; public String ButtonSelected { get; set; } public MultiButtonUserControl() { InitializeComponent(); Button1.BackgroundColor = color; } public void SetFirstButton() { Button1.BackgroundColor = color; Button2.BackgroundColor = default; Button3.BackgroundColor = default; } protected virtual void OnButtonChangedEvent() { if (ButtonChangedEvent != null) ButtonChangedEvent(this, EventArgs.Empty); } //remember that ChangeColours also triggers OnButtonChangedEvent private void ChangeColours() { if (ButtonSelected.Equals(Button1Text)) { Button1.BackgroundColor = color; Button2.BackgroundColor = default; Button3.BackgroundColor = default; } if (ButtonSelected.Equals(Button2Text)) { Button2.BackgroundColor = color; Button1.BackgroundColor = default; Button3.BackgroundColor = default; } if (ButtonSelected.Equals(Button3Text)) { Button3.BackgroundColor = color; Button1.BackgroundColor = default; Button2.BackgroundColor = default; } OnButtonChangedEvent(); } #region button text properties public string Button1Text { get { return Button1.Text; } set { Button1.Text = value; } } public string Button2Text { get { return Button2.Text; } set { Button2.Text = value; } } public string Button3Text { get { return Button3.Text; } set { Button3.Text = value; } } #endregion #region button clicked event handlers public void Button1_Clicked(object sender, EventArgs e) { ButtonSelected = Button1Text; ChangeColours(); } public void Button2_Clicked(object sender, EventArgs e) { ButtonSelected = Button2Text; ChangeColours(); } public void Button3_Clicked(object sender, EventArgs e) { ButtonSelected = Button3Text; ChangeColours(); } #endregion //this can attach bindable properties //public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(MultiButtonUserControl)); // public string Text //{ // get { return (string)GetValue(TextProperty); } // set { SetValue(TextProperty, value); } //} } }