MindFusion.Scheduling for Silverlight Programmer's Guide
Creating New Cell Presenter

In certain scenarios creating a new template is not sufficient. For example the case when you want to add an interactive control, such as Button in the cell's presenter and want to attach an event handler to this button so that you can perform specific action when it is pressed. In order to implement the above scenario, you have to derive from CellPresenter and associate a custom template with the new class. For more information on how to create custom templates, check Creating New Cell TemplateCreating New Cell Template. Somewhere within the newly created template, there will be a button definition similar to the one below.

XAML  Copy Code

<Button x:Name="myButton" />

It is important to name the button so that it can be accessed in the template class.

The definition of the custom presenter class will look similar to the following.

C#  Copy Code

class MyPresenter : CellPresenter
{
    public MyPresenter(Calendar calendar)
        : base(calendar)
    {
        this.DefaultStyleKey = typeof(MyPresenter);
    }
}

Visual Basic  Copy Code

Class MyPresenter
    Inherits CellPresenter

    Public Sub New(ByVal calendar As Calendar)
        MyBase.New(calendar)

        Me.DefaultStyleKey = GetType(MyPresenter)

    End Sub

End Class

In order to attach an event handler to the button myButton, you have to override the OnApplyTemplate method, obtain a reference to the button through the GetTemplateChild method and attach an event handler to the button's Click event. Make sure to call the method of the base class in the OnApplyTemplate override. The following code illustrates how.

C#  Copy Code

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    Button myButton = GetTemplateChild("myButton") as Button;
    if (myButton != null)
        myButton.Click += OnMyButtonClick;
}

...

void OnMyButtonClick(object sender, RoutedEventArgs e)
{
    // Perform the necessary actions here
}

Visual Basic  Copy Code

Public Overrides Sub OnApplyTemplate()

    MyBase.OnApplyTemplate()

    Dim myButton As Button = CType(GetTemplateChild("myButton"), Button)
    If Not myButton Is Nothing Then
        AddHandler myButton.Click, AddressOf OnMyButtonClick
    End If

End Sub

...

Sub OnMyButtonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)

    ' Perform the necessary actions here

End Sub

Now that the custom presenter and its template are ready, the presenter has to be associated with a cell. This can be done in the CellPresenterCreating event handler of the Calendar class.

C#  Copy Code

private void calendar_CellPresenterCreating(object sender, CellPresenterCreatingEventArgs e)
{
    e.CellPresenter = new MyPresenter(calendar);
}

Visual Basic  Copy Code

Private Sub calendar_CellPresenterCreating(ByVal sender As Object, ByVal e As CellPresenterCreatingEventArgs)

   e.CellPresenter = New HolidayPresenter(calendar)

End If

For an extensive example on how to create custom cell presenters, check the new Holidays sample.