Scheduler

The Scheduler control provides an interface for creating, viewing and editing time-based items on a calendar-style user interface. A Scheduler can present day-based, week-based or month-based views, and provides a user interface for users to select views and navigate between dates. You can use a Scheduler to show appointments, tasks, bookings and so on.

CopyExample Usage - XAML
<ms:Scheduler x:Name="SchedulerControl" />

Customising the Add and Edit Behavior

When the user creates a new schedule item, the Scheduler raises the ItemAdded event. By default, this displays a child window where the user can edit the new item (e.g. setting a name, editing the duration, setting up recurrence). To suppress the default behaviour and show your own “Add Item” user interface instead, handle the ItemAdded event. If you want to show the default user interface, but still need to be notified when items are added, set ShowDefaultEditor to true in the AddScheduleItemEventArgs.

When the user double-clicks a schedule item, the Scheduler raises the ItemActivated event. If you want to allow the user to edit the item they have double-clicked, create and show a ScheduleItemDialog, passing the ScheduleItem from the ScheduleItemEventArgs.

Setting Up the Schedule

You can access the schedule via the Schedule property. See the Schedule class for more information. Use the AddItem method to add items to the Schedule and RemoveItem to remove them.

Each item in the schedule is represented by a ScheduleItem. This contains information such as the start and end time, the name of the item, etc. You can subclass ScheduleItem to include additional information such as the priority of a task. (Note that the various “create item” UI elements in the Scheduler control itself only create ScheduleItem base class objects.)

CopyExample Usage - Code
private void AddTryTask()
{
  SchedulerControl.Schedule.AddItem(new ScheduleItem
  {
    StartTime = DateTime.Now.Date.AddHours(8),
    EndTime = DateTime.Now.Date.AddHours(9),
    Name = "Try Silverlight Elements" 
  });
}

Saving and Loading Schedules

The Scheduler control does not provide a built-in way to store or load a schedule. This is because schedules will typically be stored in a database, so as to allow querying, raising alerts, etc. Storage will therefore be dependent on the database design, the WCF service interface, etc.

To save or load the schedule, use the Scheduler.Schedule property. To read the contents of the schedule so that you can store them, use the Schedule.Items property. To populate a schedule when loading from a store, use the Schedule.AddItem method.

The following code provides a simple example of saving and loading using XML. This could for example be used to store a schedule in isolated storage on the client machine.

CopyExample - Saving and Loading a Schedule
private static void WriteSchedule(Stream stm, Schedule schedule)
{
  XElement element = new XElement("Schedule", schedule.Items.Select(si => new XElement("Item",
    new XAttribute("Name", si.Name),
    new XAttribute("StartTime", si.StartTime),
    new XAttribute("EndTime", si.EndTime),
    si.IsRecurring ?
      new XElement("Recurrence",
        new XAttribute("StartDate", si.RecurrenceInfo.StartDate),
        new XAttribute("StartTime", si.RecurrenceInfo.StartTime),
        new XAttribute("Duration", si.RecurrenceInfo.Duration),
        new XAttribute("EndDate", si.RecurrenceInfo.EndDate),
        new XAttribute("EndType", si.RecurrenceInfo.EndType),
        new XAttribute("MaxOccurrences", si.RecurrenceInfo.MaxOccurrences),
        si.RecurrenceInfo.RecurrencePattern.ToXml()
        )
      : null
    )));
  element.Save(stm);
}

private static void ReadSchedule(Stream stm, Schedule schedule)
{
  XDocument document = XDocument.Load(stm);
  IEnumerable<ScheduleItem> items = document.Elements("Schedule").Elements("Item").Select(e => new ScheduleItem
  {
    Name = (string)e.Attribute("Name"),
    StartTime = (DateTime)e.Attribute("StartTime"),
    EndTime = (DateTime)e.Attribute("EndTime"),
    RecurrenceInfo = e.Elements("Recurrence").Select(rel => new RecurrenceInfo
    (
      (DateTime)rel.Attribute("StartDate"),
      (TimeSpan)rel.Attribute("StartTime"),
      (TimeSpan)rel.Attribute("Duration"),
      rel.Elements().First().ParseRecurrencePattern(),
      (RecurrenceEndType)(Enum.Parse(typeof(RecurrenceEndType), (string)rel.Attribute("EndType"), true)),
      (DateTime)rel.Attribute("EndDate"),
      (int)rel.Attribute("MaxOccurrences")
    )).FirstOrDefault()
  });

  schedule.Clear();

  foreach (ScheduleItem item in items)
  {
    schedule.AddItem(item);
  }
}

Silverlight Elements does provide a pair of helper methods for saving recurrence information as XML, in the RecurrencePatternXmlSerializer class. It is not recommended that you save recurrence information as XML in your database, as this will make querying difficult, but these methods may be useful for creating and parsing serialisable representations to be passed to or from a WCF service, or if you are storing data locally using isolated storage. Usage of these helper methods is shown in the sample code above.