Articles in this section
Category / Section

How to select particular days from code behind in CalendarEdit

4 mins read

CalendarEdit supports multiple selection of dates in the month and this support is enabled/disabled through AllowMultiplySelection property of CalendarEdit. To select multiple dates, we need to click on each date and that collection of Dates would be added in SelectedDates property of CalendarEdit.

In this article, we are discussing about how to select multiple days without clicking each date from calendar. For instance, we have selected all days, all week days, all weekend days and selected multiple dates based on desired day from the current month. Following code examples illustrate the same,

Options Grid:

           <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5">
                 <Grid.RowDefinitions>
                      <RowDefinition Height="Auto"></RowDefinition>
                      <RowDefinition Height="Auto"></RowDefinition>
                      <RowDefinition Height="Auto"></RowDefinition>
                      <RowDefinition Height="Auto"></RowDefinition>
                      <RowDefinition Height="Auto"></RowDefinition>
                  </Grid.RowDefinitions>
                  <RadioButton x:Name="all_day" Command="{Binding SelectAllDaysinMonthCommand}" CommandParameter="{Binding ElementName=calendaredit1}" Grid.Row="0" Margin="10" Content="Select all the days in the current month"/>
                  <RadioButton x:Name="week_day" Command="{Binding SelectWeekDaysinMonthCommand}" CommandParameter="{Binding ElementName=calendaredit1}" Grid.Row="1" Margin="10" Content="Select all week days in the current month"/>
                  <RadioButton x:Name="week_end" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Command="{Binding SelectWeekendDaysinMonthCommand}" CommandParameter="{Binding ElementName=calendaredit1}" Grid.Row="2" Margin="10" Content="Select all weekend days in the current month"/>
                   <RadioButton x:Name="part_day" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Command="{Binding SelectParticularDayCommand}" CommandParameter="{Binding ElementName=calendaredit1}" Grid.Row="3" Margin="10" Content="Select particular days in the current month"/>
                   <RadioButton x:Name="clear_selecteddays" Command="{Binding ClearSelectedDaysinMonthCommand}" CommandParameter="{Binding ElementName=calendaredit1}" Margin="10" Content="Clear selected days" Grid.Row="5"/>
           </Grid>

 

Calendar Grid:

<syncfusion:CalendarEdit  Name="calendaredit1" AllowMultiplySelection="True" HorizontalAlignment="Center" VerticalAlignment="Center"/>

 

ViewModel.cs:

    public class EventViewModel : NotificationObject
    {       
        private ICommand selectalldays;
        public ICommand SelectAllDaysinMonthCommand
        {
            get
            {
                return selectalldays;
            }
            set
            {
                selectalldays = value;
                RaisePropertyChanged("SelectAllDaysinMonthCommand");
            }
        }
 
        private ICommand selectparticularday;
        public ICommand SelectParticularDayCommand
        {
            get
            {
                return selectparticularday;
            }
            set
            {
                selectparticularday = value;
                RaisePropertyChanged("SelectParticularDayCommand");
            }
        }
 
        private ICommand selectweekdaysinmonth;
        public ICommand SelectWeekDaysinMonthCommand
        {
            get
            {
                return selectweekdaysinmonth;
            }
            set
            {
                selectweekdaysinmonth = value;
                RaisePropertyChanged("SelectWeekDaysinMonthCommand");
            }
        }
 
        private ICommand selectweekenddaysinmonth;
        public ICommand SelectWeekendDaysinMonthCommand
        {
            get
            {
                return selectweekenddaysinmonth;
            }
            set
            {
                selectweekenddaysinmonth = value;
                RaisePropertyChanged("SelectWeekendDaysinMonthCommand");
            }
        }
 
        private ICommand clearSelectedDaysinMonthCommand;
 
        public ICommand ClearSelectedDaysinMonthCommand
        {
            get { return clearSelectedDaysinMonthCommand; }
            set { clearSelectedDaysinMonthCommand = value; RaisePropertyChanged("ClearSelectedDaysinMonthCommand"); }
        }
 
        public EventViewModel()
        {           
            SelectAllDaysinMonthCommand = new DelegateCommand<object>(SelectAllDays, CanSelect);
            SelectParticularDayCommand = new DelegateCommand<object>(SelectParticularDay, CanSelect);
            SelectWeekDaysinMonthCommand = new DelegateCommand<object>(SelectWeekdaysinMonth, CanSelect);
            SelectWeekendDaysinMonthCommand = new DelegateCommand<object>(SelectWeekendinMonth, CanSelect);
            ClearSelectedDaysinMonthCommand = new DelegateCommand<object>(ClearSelectedDays, CanSelect);
        }
 
        /// <summary>
        ///  Method to clear the selected dates from collection
        /// </summary>
        /// <param name="obj"></param>
        private void ClearSelectedDays(object obj)
        {
            CalendarEdit edit = obj as CalendarEdit;
            if (edit != null && edit.SelectedDates != null)
            {
                edit.SelectedDates.Clear();
                edit.SelectedDates.Add(DateTime.Now);
                edit.Date = DateTime.Now;
            }
        }
 
        private bool CanSelect(object obj)
        {
            return true;
        }
 
        /// <summary>
        /// Method to select all the dates in current month
        /// </summary>
        /// <param name="obj"></param>
        public void SelectAllDays(object obj)
        {
            CalendarEdit edit = obj as CalendarEdit;
            DateTime date = edit.Date;
            edit.SelectedDates = AllDatesInMonth(date.Year, date.Month, edit);
        }
 
        /// <summary>
        /// Method to select multiple dates based on particular day
        /// </summary>
        /// <param name="obj"></param>
        public void SelectParticularDay(object obj)
        {
            CalendarEdit edit = obj as CalendarEdit;
            DateTime date = edit.Date;
            edit.SelectedDates = SelectDay(date.Year, date.Month, date.DayOfWeek, edit);
        }
 
        /// <summary>
        /// Method to select all week days in the current month
        /// </summary>
        /// <param name="obj"></param>
        public void SelectWeekdaysinMonth(object obj)
        {
            CalendarEdit edit = obj as CalendarEdit;
            GetWeekDaysinMonth(edit);
            if (!edit.SelectedDates.Contains(edit.Date))
            {
                edit.Date = edit.SelectedDates[0];
            }
        }
 
        /// <summary>
        /// Method to select all weekend days in the current month
        /// </summary>
        /// <param name="obj"></param>
        public void SelectWeekendinMonth(object obj)
        {
            CalendarEdit edit = obj as CalendarEdit;
            GetWeekendDaysinMonth(edit);
            if (!edit.SelectedDates.Contains(edit.Date))
            {
                edit.Date = edit.SelectedDates[0];
            }
        }
 
        private DatesCollection AllDatesInMonth(int year, int month, CalendarEdit edit)
        {
            int days = DateTime.DaysInMonth(year, month);
            DatesCollection SelectedDates;
            if (edit.SelectedDates != null)
                SelectedDates = edit.SelectedDates;
            else
                SelectedDates = new DatesCollection();
            SelectedDates.Clear();
            for (int day = 1; day <= days; day++)
            {
                DateTime date = new DateTime(year, month, day);               
                SelectedDates.Add(date);
            }
            return SelectedDates;
        }
 
        private DatesCollection SelectDay(int year, int month, DayOfWeek Day, CalendarEdit edit)
        {
            int days = DateTime.DaysInMonth(year, month);
            DatesCollection SelectedDates;
            if (edit.SelectedDates != null)
                SelectedDates = edit.SelectedDates;
            else
                SelectedDates = new DatesCollection();
            SelectedDates.Clear();
            for (int day = 1; day <= days; day++)
            {
                DateTime date = new DateTime(year, month, day);
                if (date.DayOfWeek == Day)
                {               
                    SelectedDates.Add(date);
                }               
            }
            return SelectedDates;
        }
 
        private List<DateTime> GetWeekend(int year, int month)
        {
            return Enumerable.Range(1, DateTime.DaysInMonth(year, month))
                             .Select(day => new DateTime(year, month, day))
                             .Where(dt => dt.DayOfWeek == DayOfWeek.Sunday ||
                                          dt.DayOfWeek == DayOfWeek.Saturday)
                             .ToList();
        }
 
        private List<DateTime> GetDates(int year, int month)
        {
            return Enumerable.Range(1, DateTime.DaysInMonth(year, month))
                             .Select(day => new DateTime(year, month, day))
                             .Where(dt => dt.DayOfWeek != DayOfWeek.Sunday &&
                                          dt.DayOfWeek != DayOfWeek.Saturday)
                             .ToList();
        }
 
        private void GetWeekDaysinMonth(CalendarEdit edit)
        {            
            ObservableCollection<DateTime> collection = new ObservableCollection<DateTime>(GetDates(edit.Date.Year, edit.Date.Month));
            if (edit.SelectedDates != null)
            {
                edit.SelectedDates.Clear();
                foreach (DateTime date in collection)
                {                    
                    edit.SelectedDates.Add(date);
                }
                if (!edit.SelectedDates.Contains(edit.Date))
                {
                    edit.Date = edit.SelectedDates[0];
                }
            }
        }
 
        private void GetWeekendDaysinMonth(CalendarEdit edit)
        {
            ObservableCollection<DateTime> collection = new ObservableCollection<DateTime>(GetWeekend(edit.Date.Year, edit.Date.Month));
            if (edit.SelectedDates != null)
            {
                edit.SelectedDates.Clear();
                foreach (DateTime date in collection)
                {                    
                    edit.SelectedDates.Add(date);
                }
            } 
            if(!edit.SelectedDates.Contains(edit.Date))
            {
                edit.Date = edit.SelectedDates[0];
            }
        }
    }

 

Sample:

https://www.syncfusion.com/downloads/support/directtrac/general/ze/CalendarEdit_Sample295739469.zip

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied