Selection in WinUI Calendar (SfCalendar)
22 Jul 20267 minutes to read
You can change the selected date interactively by clicking on the specific date, or you can select it programmatically. By default, the Calendar control allows you to select a single date at a time.
If you want to restrict date selection or select multiple dates, set the SelectionMode property value to one of the following values:
- None - Prevents selecting a date.
- Single - Allows you to select a single date.
- Multiple - Allows you to select multiple dates.
- Range - Allows you to select a range of dates.
Single selection
You can select a single date interactively by clicking on a specific date or programmatically using the SelectedDate property. By default, the value of the SelectedDate property is null and the SelectedDates collection is empty.
using Syncfusion.UI.Xaml.Calendar;
SfCalendar sfCalendar = new SfCalendar();
sfCalendar.SelectedDate = new DateTimeOffset(new DateTime(2021, 01, 06));

If the SelectedDates collection is used instead of the SelectedDate property, the first date value in the SelectedDates collection is set as the selected date, and this value changes upon selection by interaction.
<Window
...
xmlns:calendar="using:Syncfusion.UI.Xaml.Calendar">
<Grid>
<Grid.DataContext>
<local:ViewModel x:Name="viewModel" />
</Grid.DataContext>
<calendar:SfCalendar x:Name="calendar"
SelectionMode="Single"
SelectedDates="{x:Bind viewModel.SelectedDates, Mode=TwoWay}"
/>
</Grid>
</Window>using Syncfusion.UI.Xaml.Calendar;
public class ViewModel
{
private DateTimeOffsetCollection selectedDates;
public DateTimeOffsetCollection SelectedDates
{
get { return selectedDates; }
set { selectedDates = value; }
}
public ViewModel()
{
SelectedDates = new DateTimeOffsetCollection();
SelectedDates.Add(new DateTimeOffset(new DateTime(2020, 03, 10)));
SelectedDates.Add(new DateTimeOffset(new DateTime(2020, 03, 14)));
SelectedDates.Add(new DateTimeOffset(new DateTime(2020, 03, 15)));
SelectedDates.Add(new DateTimeOffset(new DateTime(2020, 03, 21)));
SelectedDates.Add(new DateTimeOffset(new DateTime(2020, 03, 24)));
}
}NOTE
If the
SelectedDateproperty is used to select a date and theSelectionModeproperty value is Single, then theSelectedDatescollection property will have only theSelectedDateproperty value.

Multiple selection
You can select one or more dates from a different month, year, decade, or century by changing the SelectionMode property value to Multiple. You can also get the selected dates collection from the SelectedDates property. By default, the value of the SelectedDate property is null and the SelectedDates collection is empty.
NOTE
If the
SelectionModevalue is None, theSelectedDatescollection will be empty.
<Window
...
xmlns:calendar="using:Syncfusion.UI.Xaml.Calendar">
<calendar:SfCalendar Name="sfCalendar"
SelectionMode="Multiple" />
</Window>using Syncfusion.UI.Xaml.Calendar;
SfCalendar sfCalendar = new SfCalendar();
sfCalendar.SelectionMode = CalendarSelectionMode.Multiple;
NOTE
If the
SelectedDateproperty is used to select a date and theSelectionModeproperty value is Multiple, then theSelectedDatescollection property will have only theSelectedDateproperty value.
NOTE
The
SelectedDateproperty value will be the same as the first date value in theSelectedDatescollection and changes with it.

Select a date range
You can select a range of dates in the Calendar control by changing the SelectionMode property value to Range. You can also get the selected range of dates from the SelectedDates property. By default, the value of the SelectedDate property is null and the SelectedDates collection is empty.
NOTE
The
SelectedDatescollection is empty and theSelectedDateproperty value is null when a date range is selected.
<Window
...
xmlns:calendar="using:Syncfusion.UI.Xaml.Calendar">
<calendar:SfCalendar Name="sfCalendar"
SelectionMode="Range" />
</Window>using Syncfusion.UI.Xaml.Calendar;
SfCalendar sfCalendar = new SfCalendar();
sfCalendar.SelectionMode = CalendarSelectionMode.Range;

NOTE
Download demo application from Github.
Highlight today and selected date
You can highlight the today and selected date using the SelectionHighlightMode property to update the background and border of the dates. The default value of the SelectionHighlightMode property is Outline.
<Window
...
xmlns:calendar="using:Syncfusion.UI.Xaml.Calendar">
<calendar:SfCalendar x:Name="sfCalendar"
SelectionHighlightMode="Filled"
/>
</Window>using Syncfusion.UI.Xaml.Calendar;
SfCalendar sfCalendar = new SfCalendar();
sfCalendar.SelectionHighlightMode = SelectionHighlightMode.Filled;

NOTE
Download demo application from Github.
Change shape of today and selected date
You can customize the today and selected date cell shape using the SelectionShape property to customize the shape of the date cell border. The default value of the SelectionShape property is Circle.
<Window
...
xmlns:calendar="using:Syncfusion.UI.Xaml.Calendar">
<calendar:SfCalendar x:Name="sfCalendar"
SelectionShape="Rectangle"
/>
</Window>using Syncfusion.UI.Xaml.Calendar;
SfCalendar sfCalendar = new SfCalendar();
sfCalendar.SelectionShape = SelectionShape.Rectangle;

NOTE
Download demo application from GitHub.