MindFusion.Scheduling for Silverlight Programmer's Guide
Creating New Item Template

New item templates are created the same way as custom control templates - by creating a new .NET Style with TargetType set to ItemPresenter. The Style should provide a setter for the Template property. The following XAML snippet illustrates how.

XAML  Copy Code

<Style TargetType="planner:ItemPresenter" x:Key="MyStyle">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="planner:ItemPresenter">
      ...
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

The style definition needs to be placed in the application's resources (App.xaml or Application.xaml). The style needs to specify a Key which should be subsequently used to assign the style to the ItemPresenter through one of its properties - StyleKeyComplex, StyleKeySimpleHorizontal and StyleKeySimpleVertical. The Default Item Template uses this technique to provide different templates for the items in different views. Below is an example XAML code illustrating this.

XAML  Copy Code

<Style TargetType="local:ItemPresenter" x:Key="ComplexItemStyle">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="local:ItemPresenter">

        <!-- The template for Timetable and Resource view items goes here -->

      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

<Style TargetType="local:ItemPresenter" x:Key="SimpleItemHorizontalStyle">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="local:ItemPresenter">

        <!-- The template for horizontal SingleMonth, MonthRange, List and WeekRange view items goes here -->

      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

<Style TargetType="local:ItemPresenter" x:Key="SimpleItemVerticalStyle">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="local:ItemPresenter">

        <!-- The template for vertical SingleMonth, MonthRange, List and WeekRange view items goes here -->

      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

By default, the ItemPresenter is initialized with the above styles as follows:

C#  Copy Code

StyleKeyComplex = "ComplexItemStyle";
StyleKeySimpleHorizontal = "SimpleItemHorizontalStyle";
StyleKeySimpleVertical = "SimpleItemVerticalStyle";

Visual Basic  Copy Code

StyleKeyComplex = "ComplexItemStyle"
StyleKeySimpleHorizontal = "SimpleItemHorizontalStyle"
StyleKeySimpleVertical = "SimpleItemVerticalStyle"

When you define one or more new styles for the presenter you need to assign their keys to the corresponding properties. A common way to do this is by handling the ItemPresenterCreated event of the Calendar class. The following code illustrates how to assign a custom style (MyStyle) to newly created presenters:

C#  Copy Code

calendar.ItemPresenterCreated += (sender, e) =>
    {
        e.ItemPresenter.StyleKeyComplex = "MyStyle";
        e.ItemPresenter.StyleKeySimpleHorizontal = "MyStyle";
        e.ItemPresenter.StyleKeySimpleVertical = "MyStyle";
    };

For more information on how to create new templates, check Tutorial 5: Custom Item Templates.