Articles in this section
Category / Section

How to programmatically change the selected date in WPF Scheduler (Calendar)

2 mins read

Initially, the scheduler is loaded with the current day as selected date and you can initialize the scheduler with specific date by using SelectedDate property in WPF SfScheduler.

C#

Create a custom class Meeting with mandatory fields From, To, EventName and color.

public class Meeting 
{
    private string EventName;
    private Brush Color;
    private DateTime To;
    private DateTime From;
}  

C#

Create a ViewModel class and add a SelectedDate property with specific date.

 public class SchedulerViewModel : INotifyPropertyChanged
 {
        /// <summary>
        /// collecions for meetings.
        /// </summary>
        private ObservableCollection<Meeting> meetings;
 
        /// <summary>
        /// color collection.
        /// </summary>
        private ObservableCollection<Brush> colorCollection;
 
        /// <summary>
        /// current day meeting.
        /// </summary>
        private ObservableCollection<string> meetingSubjectCollection;
        private DateTime selectedDate = DateTime.Now.AddDays(5);
 
        public SchedulerViewModel()
        {
            this.Meetings = new ObservableCollection<Meeting>();
            this.AddAppointmentDetails();
            this.AddAppointments();
        }
 
         public ObservableCollection<Meeting> Meetings
        {
            get
            {
                return this.meetings;
            }
            set
            {
                this.meetings = value;
                this.RaiseOnPropertyChanged("Meetings");
            }
        }
 
        public DateTime SelectedDate
        {
            get
            {
                return this.selectedDate;
            }
            set
            {
                this.selectedDate = value;
                this.RaiseOnPropertyChanged("SelectedDate");
            }
        }
 
        /// <summary>
        /// adding appointment details.
        /// </summary>
        private void AddAppointmentDetails()
        {
            meetingSubjectCollection = new ObservableCollection<string>();
            meetingSubjectCollection.Add("BigDoor Board Meeting Paris, France");
            meetingSubjectCollection.Add("Development Meeting New York, U.S.A");
            meetingSubjectCollection.Add("Project Plan Meeting Kuala Lumpur, Malaysia");
 
            colorCollection = new ObservableCollection<Brush>();
            colorCollection.Add(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFA2C139")));
            colorCollection.Add(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFD80073")));
            colorCollection.Add(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF1BA1E2")));
           }
 
        /// <summary>
        /// Adds the appointments.
        /// </summary>
        private void AddAppointments()
        {
            var today = DateTime.Now.Date;
            var random = new Random();
            for (int month = -1; month < 2; month++)
            {
                for (int day = -5; day < 5; day++)
                {
                    for (int count = 0; count < 2; count++)
                    {
                        var meeting = new Meeting();
                        meeting.From = today.AddMonths(month).AddDays(random.Next(1, 28)).AddHours(random.Next(9, 18));
                        meeting.To = meeting.From.AddHours(1);
                        meeting.EventName = meetingSubjectCollection[random.Next(7)];
                        meeting.Color = colorCollection[random.Next(14)];
                        this.Meetings.Add(meeting);
                    }
                }
           }
      }
}

XAML

Bind the SelectedDate ViewModel property to the SelectedDate property in SfScheduler.

<Window.DataContext>
<local:SchedulerViewModel/>
</Window.DataContext>
<Grid>
<syncfusion:SfScheduler 
        x:Name="Schedule" 
        ViewType="Month"
        ItemsSource="{Binding Meetings}"
        SelectedDate="{Binding SelectedDate}">
    <syncfusion:SfScheduler.AppointmentMapping>
        <syncfusion:AppointmentMapping
                StartTime="From"
                EndTime="To"
                Subject="EventName"
                AppointmentBackground="Color">
        </syncfusion:AppointmentMapping>
    </syncfusion:SfScheduler.AppointmentMapping>
    <interactivity:Interaction.Behaviors>
        <local:SchedulerBehavior/>
    </interactivity:Interaction.Behaviors>
</syncfusion:SfScheduler>
</Grid>

View sample in GitHub

Demo image for Selected Date

 

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