How to Select a Week in the .NET MAUI Calendar (SfCalendar)?
In the Syncfusion® .NET MAUI Calendar control, select the entire week when selecting a single date using the SelectionChanged event of the SfCalendar.
Add the SfCalendar control to your layout and set its SelectionMode property to Range as shown in the following code sample.
XAML
<ContentPage.Content> <calendar:SfCalendar x:Name="calendar" SelectionMode="Range"> <calendar:SfCalendar.Behaviors> <local:CalendarBehavior /> </calendar:SfCalendar.Behaviors> </calendar:SfCalendar> </ContentPage.Content>
In the SelectionChanged event, obtain the StartDate, and calculate the first and the last day of the week based on the date. Create a new CalendarDateRange to cover the entire week, and assign the resulting date range to the SelectedDateRange property of the SfCalendar. The following code explains how to implement this step.
C#
private void Calendar_SelectionChanged(object sender, CalendarSelectionChangedEventArgs e) { int firstDayOfWeek = (int)DayOfWeek.Sunday % 7; int lastDayOfWeek = (firstDayOfWeek - 1) % 7; lastDayOfWeek = lastDayOfWeek < 0 ? 7 + lastDayOfWeek : lastDayOfWeek; CalendarDateRange range = (CalendarDateRange)e.NewValue; var startDate = (DateTime)range.StartDate; var endDate = (DateTime)(range.EndDate ?? range.StartDate); if (startDate.CompareTo(endDate) > 0) { var date = startDate; startDate = endDate; endDate = date; } int day1 = (int)startDate.DayOfWeek % 7; int day2 = (int)endDate.DayOfWeek % 7; var date1 = startDate.AddDays(firstDayOfWeek - day1); var date2 = endDate.AddDays(lastDayOfWeek - day2); if ((date1.Date != startDate) || (date2.Date != endDate)) { this.calendar.SelectedDateRange = new CalendarDateRange(date1, date2); } }
Download the complete sample on GitHub
Output:
Conclusion
I hope you enjoyed learning how to select a week in the .NET MAUI Calendar (SfCalendar).
Refer to our .NET MAUI Calendar’s feature tour page to learn about its other groundbreaking feature representations. You can explore our .NET MAUI Calendar documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI Calendar and other .NET MAUI components.
Please let us know in the following comments section if you have any queries or require clarifications. Contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!