Click here to Skip to main content
15,122,003 members
Articles / Desktop Programming / WPF
Posted 11 Nov 2008

Tagged as


74 bookmarked

Creating an Outlook Calendar Using WPF (Part 2)

Rate me:
Please Sign up or sign in to vote.
4.55/5 (21 votes)
11 Nov 2008CPOL2 min read
Part 2 in a series on how to create a replica of the Outlook Calendar (and Appointments) using WPF.

A life spent making mistakes is not only more honorable, but more useful than a life spent doing nothing.

- George Bernard Shaw


After part 1, I decided to really concentrate on how to make the Calendar control better and more “professional”. Let's assume for a minute that I wanted to sell this control. If I buy a control, I expect decent design time support, and I want the control to be stylable. My first attempt fell short!!! Let's try and make it better…

Item Generation

In the previous version of the Calendar, all the CalendarLedgerItem and CalendarTimeslotItem were created manually! This is a big no, no…

The CalendarLedger is responsible for creating all the CalendarLedgerItems. Calling PopulateLedger() will dynamically create all the ledger items!

private void PopulateLedger()
    if (_ledgerItems != null)
        for (int i = 0; i < 24; i++)
            CalendarLedgerItem item = new CalendarLedgerItem();
            item.TimeslotA = i.ToString();
            item.TimeslotB = "00";

The CalendarDay is also responsible for creating the CalendarTimeslotItems (by calling PopulateDay()).

Style, Style, Style


The Calendar currently exposes styles for the CalendarLedgerItem, CalendarTimeslotItem, and CalendarAppointmentItem.

All the styles are exposed as DependencyPropertys:

public static readonly DependencyProperty CalendarTimeslotItemStyleProperty =
                typeof(Style), typeof(Calendar));

public Style CalendarTimeslotItemStyle
    get { return (Style)GetValue(CalendarTimeslotItemStyleProperty); }
    set { SetValue(CalendarTimeslotItemStyleProperty, value); }

All that is now left to do is bind the StyleProperty of the CalendarLedgerItem, CalendarTimeslotItem, and CalendarAppointmentItem to these DependencyPropertys!



By adding the following attribute...

[assembly: XmlnsDefinition(, 

... it is now very easy to reference my controls without needing to remember all the namespaces:


Design Time Support

Visual Studio and Expression Blend design time support is currently a hot topic!


The Calendar control is actually composed of some primitives like the CalendarLedger, CalendarDay, etc. I do not want these primitives to show up in my toolbox! I only want my Calendar control to be “selectable”.

To remove controls from the toolbox, add the following attribute:


Also notice the “cool” custom icon I now have! This is achieved by adding an embedded resource with a specific name (Calendar.Icon.bmp). The icon can also be specified by using the ThumbnailAttribute!


In Expression Blend, the property grid gets divided into categories!



Each category also gets subdivided into a “Normal” section and an expander that has some “Advanced” properties.

To place a property in the “Advanced” expander:


Or to place it in the “Normal” section:


Named Parts

A common practice in designing reusable controls is to use named parts! In my Calendar control, I needed access to the CalendarDay control. To get access to this control, I started by giving it a name:

<calendarledger x:name="PART_Ledger" />

The next step is to override the Calendar's OnApplyTemplate:

CalendarDay _day;
public override void OnApplyTemplate()

    _day = GetTemplateChild(ElementDay) as CalendarDay;
    if (_day != null)
        _day.Owner = this;

In OnApplyTemplate, I try and locate the named part (by using GetTemplateChild()).

Now, I have full access to the CalendarDay control!!! The only other “thing” to notice here is that I also set the Owner of my CalendarDay. This allows my Calendar day to set bindings on its Owner:

public Calendar Owner { get; set; }

private BindingBase GetOwnerBinding(string propertyName)
    Binding result = new Binding(propertyName);
    result.Source = this.Owner;
    return result;

A best practice in using named parts is to always start the name with PART_, and also to add the following attribute to your control:

[TemplatePart(Name = CalendarLedger.ElementLedgerItems, Type = typeof(StackPanel))]

This attribute makes it easy to determine what type a named part should be!


That is it for part 2!!!

- Rudi Grobler


  • 11th November, 2008: Initial post.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

South Africa South Africa
No Biography provided

Comments and Discussions

QuestionCalendar Pin
Member 1477626119-Mar-20 6:29
MemberMember 1477626119-Mar-20 6:29 
AnswerRe: Calendar Pin
jr from Yaoundé9-Apr-20 13:43
Memberjr from Yaoundé9-Apr-20 13:43 dll (for Toolbox Browsableattribute) Not found in C# project? Pin
Jagadeeswaran Natarajan4-Nov-13 22:27
professionalJagadeeswaran Natarajan4-Nov-13 22:27 
QuestionVery nice work! Pin
Ehab K21-May-12 8:46
MemberEhab K21-May-12 8:46 
QuestionCustom datepicker missing reference Pin
PerMEriksson5-Feb-12 18:58
MemberPerMEriksson5-Feb-12 18:58 
QuestionWhy is CalendarAppointmentItem.StartTimeProperty a bool? Pin
Dr.Drew26-Dec-11 17:49
MemberDr.Drew26-Dec-11 17:49 
QuestionToolboxBrowsable should be in another assembly Pin
jibedoubleve11-Oct-11 4:20
Memberjibedoubleve11-Oct-11 4:20 
QuestionWhy is are most controls are made static Pin
tompastom10-Jul-11 1:56
Membertompastom10-Jul-11 1:56 
GeneralCompile errors abound! Please advise. Pin
Mike74915-May-11 17:47
MemberMike74915-May-11 17:47 
GeneralRe: Compile errors abound! Please advise. Pin
el0321618-May-11 0:27
Memberel0321618-May-11 0:27 
GeneralMy vote of 1 Pin
Member 72477765-Aug-10 9:35
MemberMember 72477765-Aug-10 9:35 
GeneralProblem compiling Pin
u060509414-Mar-10 22:37
Memberu060509414-Mar-10 22:37 
GeneralCan't get this to work... only header is visible, no ledger or time. Pin
Justin Time29-May-09 11:37
MemberJustin Time29-May-09 11:37 
GeneralRe: Can't get this to work... only header is visible, no ledger or time. Pin
Mtihc19-Nov-13 3:51
MemberMtihc19-Nov-13 3:51 
NewsNice start, needs more work +simple fix Pin
captainplanet012321-Jan-09 2:38
Membercaptainplanet012321-Jan-09 2:38 
QuestionVisibility by week ? Pin
supertoto0324-Nov-08 3:37
Membersupertoto0324-Nov-08 3:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.