Beispiel #1
0
        public override void Selected(UIPickerView pickerView, nint row, nint component)
        {
            if (component == YearComponentIndex)
            {
                SelectedYear = (int)pickerView.SelectedRowInComponent(YearComponentIndex);

                if (this.PickerView.CustomDatePicker?.DatePicker?.HasDay == true)
                {
                    pickerView.ReloadComponent(DayComponentIndex);
                }

                if (this.PickerView.CustomDatePicker?.DatePicker?.HasMonth == true)
                {
                    pickerView.ReloadComponent(MonthComponentIndex);
                }
            }
            else if (component == MonthComponentIndex)
            {
                SelectedMonth = (int)pickerView.SelectedRowInComponent(MonthComponentIndex);

                if (this.PickerView.CustomDatePicker?.DatePicker?.HasDay == true)
                {
                    pickerView.ReloadComponent(DayComponentIndex);
                }
            }
            else
            {
                SelectedDay = (int)pickerView.SelectedRowInComponent(DayComponentIndex);
            }

            YearModel yearModel = this.PickerView.Years[SelectedYear];

            if (yearModel.Months.Count <= 0)
            {
                return;
            }

            MonthModel monthModel = yearModel.Months[SelectedMonth];

            if (monthModel.Days.Count <= 0)
            {
                return;
            }

            SelectedDay = SelectedDay >= monthModel.Days.Count ? monthModel.Days.Count - 1 : SelectedDay;

            DayModel dayModel = monthModel.Days[SelectedDay];

            SelectedItem = yearModel.Year + "-" + monthModel.Month + "-" + dayModel.Day;

            if (!string.IsNullOrEmpty(SelectedItem))
            {
                this.PickerView.SelectedPropertyChanged(SelectedItem);
            }
        }
Beispiel #2
0
        protected override void OnElementChanged(ElementChangedEventArgs <Picker> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                return;
            }

            Control.Layer.BorderWidth = 0;
            Control.BorderStyle       = UITextBorderStyle.None;

            Years = new ObservableCollection <YearModel>();

            var startDate = DateTime.Today.AddYears(-30);

            for (int i = 0; i <= 30; i++)
            {
                var yearDate = startDate.AddYears(i);

                YearModel yearModel = new YearModel
                {
                    Year   = yearDate.Year.ToString(),
                    Months = new ObservableCollection <MonthModel>()
                };

                for (int y = 1; y <= 12; y++)
                {
                    MonthModel monthModel = new MonthModel
                    {
                        Month     = y.ToString(),
                        MonthName = new DateTime(yearDate.Year, y, 1).GetMonthName(),
                        Days      = new ObservableCollection <DayModel>()
                    };

                    var daysInMonth = DateTime.DaysInMonth(yearDate.Year, y);

                    for (int d = 1; d <= daysInMonth; d++)
                    {
                        DayModel dayModel = new DayModel
                        {
                            Day = d.ToString()
                        };

                        monthModel.Days.Add(dayModel);
                    }

                    yearModel.Months.Add(monthModel);
                }

                Years.Add(yearModel);
            }

            var pickerSource = new PickerSource(this);

            UIPickerView view = new UIPickerView
            {
                Model = pickerSource,
            };

            pickerSource.DayComponentIndex  = CustomDatePicker.DatePicker.HasDay ? 0 : -1;
            pickerSource.YearComponentIndex = (int)pickerSource.GetComponentCount(view) - 1;

            if (!CustomDatePicker.DatePicker.HasDay && CustomDatePicker.DatePicker.HasMonth)
            {
                pickerSource.MonthComponentIndex = 0;
            }
            else if (CustomDatePicker.DatePicker.HasMonth)
            {
                pickerSource.MonthComponentIndex = 1;
            }
            else
            {
                pickerSource.MonthComponentIndex = -1;
            }

            if (CustomDatePicker.DatePicker.HasYear)
            {
                view.Select(pickerSource.SelectedYear, pickerSource.YearComponentIndex, false);
            }

            if (CustomDatePicker.DatePicker.HasMonth)
            {
                view.Select(pickerSource.SelectedMonth, pickerSource.MonthComponentIndex, false);
            }

            if (CustomDatePicker.DatePicker.HasDay)
            {
                view.Select(pickerSource.SelectedDay, pickerSource.DayComponentIndex, false);
            }

            Control.InputView = view;
        }