123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Collections.Specialized;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Essentials;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- namespace comal.timesheets
- {
- public delegate void CustomPickeriOSValueChanged();
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class CustomPickeriOS : ContentView
- {
- public event CustomPickeriOSValueChanged CustomPickeriOSValueChanged;
- public string Title { get; set; }
- public List<string> StringList { get; set; }
- public int SelectedIndex { get; set; }
- public string SelectedItem { get; set; }
- List<PickerOption> Options = new List<PickerOption>();
- double fixedHeight = 0;
- public CustomPickeriOS()
- {
- InitializeComponent();
- Title = "";
- SelectedItem = "";
- StringList = new List<string>();
- SelectedIndex = -1;
- var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
- fixedHeight = (mainDisplayInfo.Height / 4) * 3;
- }
- public void SetDefault(string s)
- {
- selectBtn.Text = s;
- }
- public void SetBackGroundColor(Color color)
- {
- selectBtn.BackgroundColor = color;
- }
- public void AddItems(List<string> items, string defaultText = "")
- {
- foreach (string s in items)
- {
- StringList.Add(s);
- PickerOption option = new PickerOption { OptionString = s };
- Options.Add(option);
- }
- listView.ItemsSource = Options;
- if (!string.IsNullOrWhiteSpace(defaultText))
- {
- selectBtn.Text = defaultText;
- SelectedItem = defaultText;
- }
- }
- void SelectBtn_Clicked(object sender, EventArgs e)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- double dynamicHeight = 30 + StringList.Count * 20;
- if (dynamicHeight > fixedHeight)
- ((this.Parent) as View).HeightRequest = fixedHeight;
- else
- ((this.Parent) as View).HeightRequest = dynamicHeight;
- listViewRow.Height = new GridLength(1, GridUnitType.Auto);
- buttonRow.Height = new GridLength(0, GridUnitType.Absolute);
- listView.IsVisible = true;
- selectBtn.IsVisible = false;
- });
- }
- void Cell_Tapped(object sender, EventArgs e)
- {
- PickerOption option = listView.SelectedItem as PickerOption;
- SelectedIndex = StringList.IndexOf(option.OptionString);
- selectBtn.Text = option.OptionString;
- listView.IsVisible = false;
- listViewRow.Height = new GridLength(0, GridUnitType.Absolute);
- selectBtn.IsVisible = true;
- SelectedItem = option.OptionString;
- buttonRow.Height = new GridLength(50, GridUnitType.Absolute);
- ((this.Parent) as View).HeightRequest = 50;
- CustomPickeriOSValueChanged?.Invoke();
- }
- }
- public class PickerOption
- {
- public string OptionString { get; set; }
- public PickerOption()
- {
- OptionString = "";
- }
- }
- }
|