This tutorial shows how to create a custom schedule item class. For simplicity, the sample demonstrates that by inheriting a class from Appointment.
Follow steps 1 through 3 from Tutorial 1: Getting Started.
Select Project -> Add Class... from the menu, type the name of the custom item class in the dialog that appears (for example MyApp) and click 'OK'. Include the MindFusion.Scheduling namespace at the beginning of the file.
C#
![]() |
---|
using MindFusion.Scheduling; |
Visual Basic
![]() |
---|
Imports MindFusion.Scheduling |
The custom class will expose a new property of type Boolean (called Kept) that will indicate whether the appointment has been kept or cancelled. The implementation of the property is trivial. Simply add a private field of type Boolean and make the property getter setter to return the field and to set the field respectively. The following sample code illustrates this:
C#
![]() |
---|
public class MyApp : Appointment |
Visual Basic
![]() |
---|
Class MyApp |
![]() |
---|
You must provide constructor that takes no argument with your custom item class. MindFusion.Scheduling for WPF creates item class instances using reflection and an exception will be thrown if there isn't such constructor available. |
By default the Calendar control instantiates items of type Appointment when the user creates items interactively (by typing). To make the calendar create items of the new class, add the following line of code in the window's constructor.
C#
![]() |
---|
calendar.InteractiveItemType = typeof(MyApp); |
Visual Basic
![]() |
---|
calendar.InteractiveItemType = GetType(MyApp) |
Now all items created by the user at runtime will be instances of MyApp.
To test whether the calendar actually creates MyApp instances, add an event handler to the control's ItemClick event. Add the following as a body of the event handler:
C#
![]() |
---|
if (e.Item is MyApp) |
Visual Basic
![]() |
---|
If TypeOf e.Item Is MyApp Then |
Clicking on an item now should display the message box.
Serialization of custom items requires additional efforts. Attempts to save a schedule containing items of type MyApp will result in an InvalidOperationException exception. In order to support serialization, we have to register MyApp with the schedule. For this purpose, add the following line of code somewhere in the window's constructor:
C#
![]() |
---|
Schedule.RegisterItemClass(typeof(MyApp), "myapp", 1); |
Visual Basic
![]() |
---|
Schedule.RegisterItemClass(GetType(MyApp), "myapp", 1) |
Now the exception won't be thrown, but you still need to override the SaveTo and LoadFrom methods of the Appointment class in order to serialize the custom property Kept. The code below illustrates how this is done.
C#
![]() |
---|
public override void SaveTo(XmlElement element, XmlSerializationContext context) |
Visual Basic
![]() |
---|
Public Overrides Sub SaveTo(ByVal element As XmlElement, ByVal context As XmlSerializationContext) |
Recurrence of items from a custom class is not handled wholly automatically. Let's take a closer look at how recurrence works in MindFusion.Scheduling for WPF. When a recurrence pattern is associated with an existing item, the item is said to become the master of the recurrence. Every time the occurrences of a master item must be displayed in a given time range, they are generated by instantiating new objects of the same type as that of the master. This is necessary in order to support infinite recurrences, where the generation of all recurrence instances is not possible. When the occurrences are generated, their properties are copied from the master item. This is done automatically for the built-in item properties, but not for those added by a derived item class. In order to copy the custom properties from the master item to a particular occurrence, you need to override the CopyOccurrence method. The following code sample shows how to do this.
C#
![]() |
---|
protected override void CopyOccurrence(Item master) |
Visual Basic
![]() |
---|
Protected Overloads Overrides Sub CopyOccurrence(ByVal master As Item) |
In addition, it is recommended to mark an item from a recurrent series as an exception when the value of a custom property changes. Otherwise, the change will be lost the next time the instance is generated.
C#
![]() |
---|
//... |
Visual Basic
![]() |
---|
' ... |
The users can clone items by holding down a particular key (the SHIFT key by default). To enable your custom items for interactive cloning, you have to override the Clone method, create a new object from your custom type and duplicate the fields of the prototype item. If you do not override the Clone method, MindFusion.Scheduling for WPF will still create copies of the item, but they will be of type Appointment instead of the custom item type. Here is a sample implementation of the Clone method.
C#
![]() |
---|
//... |
Visual Basic
![]() |
---|
'... |