Articles in this section
Category / Section

How to update ItemsSource collection in WinRT Schedule?

3 mins read

You can update the local appointment collection by using the editor events in the SfSchedule control. This article explains how to update the local appointment collection by using the default editor.

Updating the local appointment collection by using the default editor:

  1. Create a WinRT application and add the SfSchedule control to it.

XAML

<syncfusion:SfSchedule x:Name="sched" ScheduleType="Month" >
 </syncfusion:SfSchedule>
  1. Create the class for declaring the custom appointment properties. To listen to the properties dynamically, implement the property contained class in the INotifyPropertyChanged interface as given in the following code example.

C#

public class clsAppointment : INotifyPropertyChanged
        {
            private bool m_AllDay;
            public bool AllDay
            {
                get { return m_AllDay; }
                set { m_AllDay = value; OnPropertyChanged("AllDay"); }
            }
            private System.DateTime m_EndDateTime;
            public System.DateTime EndDateTime
            {
                get { return m_EndDateTime; }
                set { m_EndDateTime = value; OnPropertyChanged("EndDateTime"); }
            }
            private string m_Location;
            public string Location
            {
                get { return m_Location; }
                set { m_Location = value; OnPropertyChanged("Location"); }
            }
            private string m_Notes;
            public string Notes
            {
                get { return m_Notes; }
                set { m_Notes = value; OnPropertyChanged("Notes"); }
            }
            private System.DateTime m_StartDateTime;
            public System.DateTime StartDateTime
            {
                get { return m_StartDateTime; }
                set { m_StartDateTime = value; OnPropertyChanged("StartDateTime"); }
            }
            private string m_Subject;
            public string Subject
            {
                get { return m_Subject; }
                set { m_Subject = value; OnPropertyChanged("Subject"); }
            }
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string propertyName)
            {
                var eventHandler = this.PropertyChanged;
                if (eventHandler != null)
                    eventHandler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
  1. Create the local appointment collection. To listen to the collection dynamically, use the ObservableCollection as given in the following code example.

C#

   private ObservableCollection<clsAppointment> m_Source = new         ObservableCollection<clsAppointment>();
        public ObservableCollection<clsAppointment> Source
        {
            get { return m_Source; }
            set { m_Source = value; }
        }
  1. Now, create the AppointmentEditorClosed event that is based on the e.Action value of the event argument, where you can perform the actions by setting the e.Handled as true to avoid the default action done by the editor.

C#

private void sched_AppointmentEditorClosed(object sender, AppointmentEditorClosedEventArgs e)
        {
            if (e.Action == EditorClosedAction.Save)
            {
                if (e.IsNew)
                {
                    AddNewClsAppointment(e.EditedAppointment);
                }
                else
                {
                    EditClsAppointment(e.EditedAppointment);
                }
            }
            if (e.Action == EditorClosedAction.Delete)
            {
                RemoveClsAppointment(e.OriginalAppointment);
            }
            e.Handled = true;
        }
private void EditClsAppointment(object appointment)
        {
            if(appointment is ScheduleAppointment)
            {
                var scheduleAppointment = appointment as ScheduleAppointment;
                //Since the appointment's ObjectID is equal to the hashCode value of the corresponding CustomClass object in the ItemsSource, based on it, the corresponding item is updated.
                var clsAppointment = Source.FirstOrDefault(app => app.GetHashCode() == scheduleAppointment.ObjectID);
                UpdateClsAppointment(scheduleAppointment, clsAppointment);   
            }
        }
        private void AddNewClsAppointment(object appointment)
        {
            if(appointment is ScheduleAppointment)
            {
                var scheduleAppointment = appointment as ScheduleAppointment;
                var clsAppointment = new clsAppointment();
                UpdateClsAppointment(scheduleAppointment, clsAppointment);
                Source.Add(clsAppointment);
            }
        }
private void RemoveClsAppointment(object appointment)
        {
            if (appointment is ScheduleAppointment)
            {
                var scheduleAppointment = appointment as ScheduleAppointment;
                //Since the appointment's ObjectID is equal to the hashCode value of the corresponding CustomClass object in ItemsSource, based on it, the corresponding item is updated.
               var clsAppointment = Source.FirstOrDefault(app => app.GetHashCode() == scheduleAppointment.ObjectID);
               Source.Remove(clsAppointment);
            }
        }
        private static void UpdateClsAppointment(ScheduleAppointment scheduleAppointment, LocalAppointmentCollection_WinRT.clsAppointment clsAppointment)
        {
                clsAppointment.Subject=scheduleAppointment.Subject;
                clsAppointment.AllDay = scheduleAppointment.AllDay;
                clsAppointment.Location = scheduleAppointment.Location;
                clsAppointment.Notes = scheduleAppointment.Notes;
                clsAppointment.StartDateTime = scheduleAppointment.StartTime;
                clsAppointment.EndDateTime = scheduleAppointment.EndTime;
        }

In the above code example, by using the e.EditedAppointment and the e.OriginalAppointment value, you can update the custom appointment collection.

  1. Now, create the ScheduleAppointmentMapping as given in the following code example.

XAML

           <syncfusion:SfSchedule.AppointmentMapping>
                <syncfusion:ScheduleAppointmentMapping SubjectMapping="Subject" NotesMapping="Notes" LocationMapping="Location" StartTimeMapping="StartDateTime"
                      EndTimeMapping="EndDateTime" AllDayMapping="AllDay" />
            </syncfusion:SfSchedule.AppointmentMapping>

After completing the above steps, run the project and the local appointment collection gets updated when you Add, Edit, or Delete an appointment. Use the Edit option in the context menu to edit or delete appointments.

 

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