This is a migrated thread and some comments may be shown as answers.

Selected day button issue

3 Answers 198 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 21 Mar 2018, 09:45 PM

I'm using a RadCalendar in a user control with SelectionMode set to Single, DisplayMode set to MonthView, a DayTemplateSelector set like your example of this in your demo. I also have it handling the SelectionChanged event to change the date values in the DayTemplateSelector. Basically I am using a DayTemplateSelector to change the template for the Day based on a collection of appointment dates to look either selected (the scheduled appointment date) or crossed off with an X (an appointment date to be excluded). This is supposed to be modified when the user left clicks on a Day to X it off or exclude it (through the SelectionChanged event handler). Initially I wanted to use a double-click for selection but there is no double-click event to handle and the double-click example I found in another thread here required adding a huge block of xml for the Calendar Button Style and it wouldn't work in our project due to unresolved reference errors (the sample worked but it wouldn't work in our larger real-world project).

The calendar control should only allow a single day to be selected since the SelectionMode is single but each click on a Day seems to select another Day. Why? I tried setting the SelectedDate value to null from the SelectionChanged event to hopefully clear the selections but this seems to have no effect. This seems to result in peculiar looking calendar as eventually multiple days appear to be selected as the user clicks to "exclude" scheduled appointment dates. Either I need a way to ensure only one of the days is selected or I need a way to clear the selections somehow from the SelectionChanged event handler.

Can you please offer some guidance?

 

Thanks!

 

Is there a way to clear the selected day buttons?

3 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 23 Mar 2018, 01:28 PM
Hello James,

Could you please clarify how exactly you're handling the SelectionChanged event at the moment? I tested the behavior you described with the Selection Range and Constraints demo from our WPF Controls Demos as I believe this is the one you were refering to and the single selection works as expected at my end. If you had another demo in mind, please let me know.

Ideally, would it be possible for you to share the whole code of your current implementation so that I can reproduce the issue at my end and assist you further?

I look forward to your reply.

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
James
Top achievements
Rank 1
answered on 10 Apr 2018, 03:24 PM

Thanks for the reply. Apologies for my delay. I got overloaded with other tasks. I am trying to accomplish using the calendar control to allow a user to see a set of scheduled dates highlighted and then allow the user to ideally double-click to exclude one of the scheduled dates. Since I couldn't use a double-click because there's no event for that (and the example I found wound up being to include a bunch of xaml override stuff that didn't work with our themes in our project), I am trying to use single-click. This kind of works but I cannot seem to get the control to just unselect a date after the user clicks it (either excluding the date or re-including it--they can only click to exclude or re-include scheduled dates). I tried setting SelectedDate to null and that didn't work so I tried setting it to just today's date and that didn't work either. It seems to just keep adding each date button to the selection visually even though the SelectedDate is still just today. Here is my Selection changed handler (view model just contains the same collections that the custom DayButtonTemplateSelector object contains and the DayButtonTemplateSelector is basically just slightly modified from the example to have ExcludedDays as well as BookedDays) :

 

            if (bLoaded)
            {
                RecurrenceOptionsViewModel vm = DataContext as RecurrenceOptionsViewModel;
                if (vm != null)
                {
                    // if there is a selected date
                    if (calendar.SelectedDate.HasValue)
                    {
                        // get the selected date
                        DateTime d = calendar.SelectedDate.Value;
                        if (d != null)
                        {
                            // if user attempted to exclude / re-include a date prior to today
                            if (d.Date < DateTime.Today.Date)
                                // disallow and return
                                return;

                            // access the calendar control's template selector
                            UsingDayTemplateSelector.DayButtonTemplateSelector ds = (UsingDayTemplateSelector.DayButtonTemplateSelector)calendar.DayTemplateSelector;

                            if (ds != null)
                            {
                                // if booked day
                                if (ds.BookedDays.Contains(d))
                                {
                                    // remove booking
                                    ds.BookedDays.Remove(d);

                                    vm.SelectedCalendarDates.Remove(d);

                                    if (!ds.ExcludedDays.Contains(d))
                                    {
                                        // add exclusion
                                        ds.ExcludedDays.Add(d);

                                        if (!vm.ExcludedDates.Contains(d))
                                            // add excluded date to view model
                                            vm.ExcludedDates.Add(d);
                                    }
                                }
                                else
                                {
                                    if (ds.ExcludedDays.Contains(d))
                                    {
                                        if (!ds.BookedDays.Contains(d))
                                        {
                                            // add booked day
                                            ds.BookedDays.Add(d);

                                            if (!vm.SelectedCalendarDates.Contains(d))
                                                // add booked day to view model
                                                vm.SelectedCalendarDates.Add(d);
                                        }

                                        // remove excluded day
                                        ds.ExcludedDays.Remove(d);

                                        // remove excluded day from view model
                                        vm.ExcludedDates.Remove(d);
                                    }
                                }

                                // update the template selector (refreshes control layout)
                                calendar.DayTemplateSelector = null;
                                calendar.DayTemplateSelector = ds;

                                // remove calendar selections
                                calendar.SelectedDate = DateTime.Today;
                            }
                        }
                    }
                }
            }
        }

 

 

0
Martin Ivanov
Telerik team
answered on 13 Apr 2018, 11:30 AM
Hello James,

I checked your code but I wasn't able to integrate it with the example. May I ask you to try isolating the issue in a runnable project and share all code snippets required to run the project locally. This will allow me to test this and check what is going on.

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
Calendar
Asked by
James
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
James
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or