Show preset items in drop-down calendar of Calendar DateRange Picker
22 Jul 20269 minutes to read
Show preset items in drop-down
You can show a collection of preset items in the drop-down of the Calendar DateRange Picker control using the Preset and PresetTemplate properties. Bind the collection of preset items to be displayed to the Preset property and set the required template in the PresetTemplate property.
using Syncfusion.UI.Xaml.Calendar;
//ViewModel class
class ViewModel
{
public ObservableCollection<string> PresetCollection { get; set; }
public ViewModel()
{
PresetCollection = new ObservableCollection<string>();
PresetCollection.Add("This Week");
PresetCollection.Add("This Month");
PresetCollection.Add("Last Month");
PresetCollection.Add("This Year");
PresetCollection.Add("Custom Range");
}
}Add the Syncfusion.UI.Xaml.Calendar namespace reference in code-behind.
<Window
...
xmlns:calendar="using:Syncfusion.UI.Xaml.Calendar">
<Grid>
<Grid.DataContext>
<local:ViewModel x:Name="viewModel" />
</Grid.DataContext>
<calendar:SfCalendarDateRangePicker x:Name="sfCalendarDateRangePicker" Height="35" Width="200"
Preset="{x:Bind viewModel.PresetCollection, Mode=TwoWay}" >
<calendar:SfCalendarDateRangePicker.PresetTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding}" SelectionChanged="ListBox_SelectionChanged" />
</DataTemplate>
</calendar:SfCalendarDateRangePicker.PresetTemplate>
</calendar:SfCalendarDateRangePicker>
</Grid>
</Window>using Syncfusion.UI.Xaml.Calendar;
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
DateTimeOffset todayDate = DateTimeOffset.Now;
if (listBox.SelectedItem.ToString() == "This Week")
{
DateTimeOffset startdate = todayDate.AddDays(-(todayDate.DayOfWeek - (DayOfWeek)sfCalendarDateRangePicker.FirstDayOfWeek));
sfCalendarDateRangePicker.SelectedRange = new DateTimeOffsetRange(startdate, startdate.AddDays(6));
}
else if (listBox.SelectedItem.ToString() == "This Month")
{
DateTimeOffset startdate = todayDate.AddDays(-(todayDate.Date.Day - 1));
int daysToAdd = DateTime.DaysInMonth(startdate.Year, startdate.Month) - 1;
DateTimeOffset lastDateInMonth = startdate.AddDays(daysToAdd);
sfCalendarDateRangePicker.SelectedRange = new DateTimeOffsetRange(startdate, lastDateInMonth);
}
else if (listBox.SelectedItem.ToString() == "Last Month")
{
DateTimeOffset startdate = todayDate.AddMonths(-1).AddDays(-(todayDate.Date.Day - 1));
int daysToAdd = DateTime.DaysInMonth(startdate.Year, startdate.Month) - 1;
DateTimeOffset lastDateInMonth = startdate.AddDays(daysToAdd);
sfCalendarDateRangePicker.SelectedRange = new DateTimeOffsetRange(startdate, lastDateInMonth);
}
else if (listBox.SelectedItem.ToString() == "This Year")
{
DateTimeOffset startdate = todayDate.AddMonths(-(todayDate.Month - 1)).AddDays(-(todayDate.Date.Day - 1));
int daysToAdd = DateTime.DaysInMonth(startdate.Year, startdate.Month) - 1;
DateTimeOffset lastDateInLastMonth = startdate.AddMonths(11).AddDays(daysToAdd);
sfCalendarDateRangePicker.SelectedRange = new DateTimeOffsetRange(startdate, lastDateInLastMonth);
}
else
{
sfCalendarDateRangePicker.SelectedRange = null;
}
}
Hide calendar on selecting a preset item
You can hide the calendar in the drop-down when a user selects any preset items in the drop-down other than CustomRange using the ShowCalendar property. When a user wants to select a custom range of dates, the calendar is added to the drop-down of the Calendar DateRange Picker control after selecting the CustomRange preset item.
using Syncfusion.UI.Xaml.Calendar;
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
sfCalendarDateRangePicker.ShowCalendar = false;
DateTimeOffset todayDate = DateTimeOffset.Now;
if (listBox.SelectedItem.ToString() == "This Week")
{
DateTimeOffset startdate = todayDate.AddDays(-(todayDate.DayOfWeek - sfCalendarDateRangePicker.FirstDayOfWeek));
sfCalendarDateRangePicker.SelectedRange = new DateTimeOffsetRange(startdate, startdate.AddDays(6));
}
else if (listBox.SelectedItem.ToString() == "This Month")
{
DateTimeOffset startdate = todayDate.AddDays(-(todayDate.Date.Day - 1));
int daysToAdd = DateTime.DaysInMonth(startdate.Year, startdate.Month) - 1;
DateTimeOffset lastDateInMonth = startdate.AddDays(daysToAdd);
sfCalendarDateRangePicker.SelectedRange = new DateTimeOffsetRange(startdate, lastDateInMonth);
}
else if (listBox.SelectedItem.ToString() == "Last Month")
{
DateTimeOffset startdate = todayDate.AddMonths(-1).AddDays(-(todayDate.Date.Day - 1));
int daysToAdd = DateTime.DaysInMonth(startdate.Year, startdate.Month) - 1;
DateTimeOffset lastDateInMonth = startdate.AddDays(daysToAdd);
sfCalendarDateRangePicker.SelectedRange = new DateTimeOffsetRange(startdate, lastDateInMonth);
}
else if (listBox.SelectedItem.ToString() == "This Year")
{
DateTimeOffset startdate = todayDate.AddMonths(-(todayDate.Month - 1)).AddDays(-(todayDate.Date.Day - 1));
int daysToAdd = DateTime.DaysInMonth(startdate.Year, startdate.Month) - 1;
DateTimeOffset lastDateInLastMonth = startdate.AddMonths(11).AddDays(daysToAdd);
sfCalendarDateRangePicker.SelectedRange = new DateTimeOffsetRange(startdate, lastDateInLastMonth);
}
else
{
sfCalendarDateRangePicker.SelectedRange = null;
sfCalendarDateRangePicker.ShowCalendar = true;
}
}NOTE
Download demo from GitHub.