Click here to Skip to main content
6,292,811 members and growing! (10,105 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Controls     Intermediate License: The Code Project Open License (CPOL)

Creating an Outlook Calendar Using WPF (Part 2)

By rudigrobler

Part 2 in a series on how to create a replica of the Outlook Calendar (and Appointments) using WPF.
C#, Windows, .NET, WPF, Dev, Design
Posted:11 Nov 2008
Views:10,920
Bookmarked:36 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
13 votes for this article.
Popularity: 4.82 Rating: 4.32 out of 5

1
1 vote, 7.7%
2

3
5 votes, 38.5%
4
7 votes, 53.8%
5

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

- George Bernard Shaw

Introduction

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";
            item.SetBinding(CalendarLedgerItem.StyleProperty, 
            GetOwnerBinding("CalendarLedgerItemStyle"));
            _ledgerItems.Children.Add(item);
        }
    }
}

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

Style, Style, Style

CalendarProperties.jpg

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

All the styles are exposed as DependencyPropertys:

public static readonly DependencyProperty CalendarTimeslotItemStyleProperty =
    DependencyProperty.Register("CalendarTimeslotItemStyle", 
                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!

timeslot.SetBinding(CalendarTimeslotItem.StyleProperty, 
        GetOwnerBinding("CalendarTimeslotItemStyle"));

Namespaces

By adding the following attribute...

[assembly: XmlnsDefinition(http://schemas.rudigrobler.com/wpf/2008, 
    "RudiGrobler.Controls")]

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

xmlns:rg=”http://schemas.rudigrobler.com/wpf/2008” 

Design Time Support

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

<>CalendarInToolbox.jpg

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:

[ToolboxBrowsable(false)]

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!

CalendarIcon.jpg

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

[Category("Calendar")] 

CalendarProperties.jpg

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:

[EditorBrowsable(EditorBrowsableState.Advanced)]

Or to place it in the “Normal” section:

[EditorBrowsable(EditorBrowsableState.Always)]

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()
{
    base.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!

Summary

That is it for part 2!!!

- Rudi Grobler

History

  • 11th November, 2008: Initial post.

License

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

About the Author

rudigrobler


Member

Location: South Africa South Africa

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralCan't get this to work... only header is visible, no ledger or time. PinmemberJustin Time11:37 29 May '09  
NewsNice start, needs more work +simple fix Pinmembercaptainplanet01232:38 21 Jan '09  
GeneralVisibility by week ? Pinmembersupertoto033:37 24 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Nov 2008
Editor: Smitha Vijayan
Copyright 2008 by rudigrobler
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project