Custom SfTimePicker I would like update collection on opening

Hi,

I am using your custom SfTimePicker. What I would like to do is change the available times to be selected when other variables have been selected. So far I can do this only via the selectionchanged function. However this does not work as I want to do it when the picker opens not when the user changes the selected time. Is there a way to do this?


my code so far in selectionchanged:

private void CustomTimePicker_SelectionChanged(object sender, Syncfusion.SfPicker.XForms.SelectionChangedEventArgs e)

        {

            SelectedDate = Preferences.Get("Selected_Key", DateTime.Today);

            minFormat = DateTime.Now.ToString("tt", CultureInfo.InvariantCulture);

            var newdate = Preferences.Get("Newdate", true);


            if (DateTime.Today == SelectedDate)

            {

                minHourAm = DateTime.Now.AddHours(2).Hour;

                if (minFormat == "AM" && minHourAm <= 11 || minFormat == "PM")

                {

                    (e.NewValue as IList)[2] = "PM";

                    minHourPM = DateTime.Now.AddHours(2).Hour;

                }

            }

            else

            {

                minHourAm = 4;

                minHourPM = 12;

            }


            if (Time.Count == 3)

            {

                bool flag = false;

                if (e.OldValue != null && e.NewValue != null && (e.NewValue as ObservableCollection<object>).Count == 3)

                {

                    if (!initialLoad || (!object.Equals((e.OldValue as IList)[2], (e.NewValue as IList)[2])))

                    {

                        flag = true;

                        initialLoad = true;

                    }


                }


                if (flag || newdate)

                {

                    ObservableCollection<object> hours = new ObservableCollection<object>();


                    if (((e.NewValue as IList)[2].ToString()) == "AM")

                    {

                        for (int i = minHourAm; i <= 11; i++)

                        {

                            if (i < 10)

                            {

                                hours.Add("0" + i);

                            }

                            else

                            {

                                hours.Add(i.ToString());

                            }

                        }

                    }


                    else if (((e.NewValue as IList)[2].ToString()) == "PM")

                    {


                        for (int i = minHourPM; i <= 22; i++)

                        {

                            if (i > 12)

                            {

                                var j = (i - 12);

                                if (j < 10)

                                {

                                    hours.Add("0" + j);

                                }

                                else

                                {

                                    hours.Add(j.ToString());

                                }

                            }

                            else

                                hours.Add(i.ToString());


                        }

                    }


                    ObservableCollection<object> PreviousValue = new ObservableCollection<object>();


                    foreach (var item in e.NewValue as IList)

                    {

                        PreviousValue.Add(item);

                    }


                    if (Time.Count > 0)

                    {

                        Time.RemoveAt(0);

                        Time.Insert(0, hours);

                    }


                    if(newdate)

                    {

                        this.SelectedItem = null;

                    }

                    else if ((Time[0] as IList).Contains(PreviousValue[0]))

                    {

                        this.SelectedItem = PreviousValue;

                    }

                    else

                    {

                        PreviousValue[1] = (Time[0] as IList)[(Time[0] as IList).Count - 1];

                        this.SelectedItem = PreviousValue;

                    }

                    bool Newdate = false;

                    Preferences.Set("Newdate", Newdate);

                }

            }


        }


1 Reply

RS Ruba Shanmugam Syncfusion Team April 22, 2022 04:14 PM UTC

Hi Zain,


In SfPicker we have the Opened and Closed events to get the popup notifications. We have prepared a sample by using a custom SfTimePicker and we have set the SelectedItem in the SfPicker Opened event. In the sample, we have updated the Picker selection on opening the Picker. Please get the sample from the attachment.


Could you please check the sample and If your requirement is different then modify the sample and share the issue replication steps or video so that it will be helpful for us to assist you further?


Please have the sample from the attachment. Let us know if you have any other concerns.


Regards,

Ruba Shanmugam


Attachment: TimePicker_Selection_90b8780f.zip

Loader.
Up arrow icon