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"> |
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"> |
By default, the ItemPresenter is initialized with the above styles as follows:
C# Copy Code |
---|
StyleKeyComplex = "ComplexItemStyle"; |
Visual Basic Copy Code |
---|
StyleKeyComplex = "ComplexItemStyle" |
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) => |
For more information on how to create new templates, check Tutorial 5: Custom Item Templates.