We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to select particular days from code behind in CalendarEdit


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

 

 

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile