Click here to Skip to main content
6,304,948 members and growing! (18,683 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 1)

By rudigrobler

Recreate the Outlook Calendar using WPF.
C# (C# 1.0, C# 2.0, C# 3.0), XML, Windows, .NET, Office, WPF, Dev, Design
Posted:21 Oct 2008
Views:26,068
Bookmarked:107 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
39 votes for this article.
Popularity: 7.29 Rating: 4.58 out of 5

1
2 votes, 5.1%
2
1 vote, 2.6%
3
7 votes, 17.9%
4
29 votes, 74.4%
5

Introduction

Microsoft Office is undoubtedly one of the best selling products in the world today! Thy always try and innovate… In this CodeProject article, I will try and recreate the Microsoft Outlook Calendar control using WPF.

All elements/controls in WPF are look-less! This reduces the need to create custom controls. A button is an element that supports clicking on it and then raising a Click event, but there is no restriction on how this button should look!

So, with my WPF think cap on, I tried to find a control that I could restyle to fit my calendar control… After trying out a few ideas, I decided to rather create a custom control.

Here are the basic elements of my custom Calendar control:

CalendarLedgerItem and CalendarLedger

Ledger.jpg

The ledger indicates the timeslot time.

CalendarTimeslotItem

Timeslot.jpg

Each day is divided into 30 minute slots (represented by CalendarTimeslotItem). CalendarTimeslotItem provides a very simple hover style to hint to the user that by clicking on the timeslot, you can add an appointment. The CalendarTimeslotItem also exposes (and raises) AddAppointmentEvent, which gets bubbled to the calendar (by clicking on the timeslot). CalendarTimeslotItem derives from ButtonBase.

public static readonly RoutedEvent AddAppointmentEvent = 
    EventManager.RegisterRoutedEvent("AddAppointment", RoutingStrategy.Bubble, 
    typeof(RoutedEventHandler), typeof(CalendarTimeslotItem));

CalendarAppointmentItem

AppointmentItem.jpg

CalendarAppointmentItem is a very generic container to show an appointment (similar to ListboxItem). I cheat a little by assuming that what every “item” CalendarDay binds to will have a StartTime and EndTime property!

<Setter Property="StartTime" Value="{Binding StartTime}" />
<Setter Property="EndTime" Value="{Binding EndTime}" />

[Note] I know this sucks a little… I will address this in part 2!

CalendarDay

The CalendarDay is the heart of our Outlook calendar. The CalendarDay derives from ItemsControl.

protected override DependencyObject GetContainerForItemOverride()
{            
    return new CalendarAppointmentItem();
}

protected override bool IsItemItsOwnContainerOverride(object item)
{
    return (item is CalendarAppointmentItem);
}

Each “item” added to the CalendarDay will implicitly have CalendarAppointmentItem as its container. This will all “magically” work, provided that the objects bound to the ItemsControl have the StartTime and EndTime properties!

TimeslotPanel

The last part we have to cover before we look at the calendar control is the TimeslotPanel. This custom layout panel will position each “item” in the CalendarDay control based on its start and end times!

<ItemsPanelTemplate>
    <local:TimeslotPanel />
</ItemsPanelTemplate>

Calendar

The Calendar adds an owner to the CalendarTimeslotItem.AddAppointmentEvent.

public static readonly RoutedEvent AddAppointmentEvent = 
    CalendarTimeslotItem.AddAppointmentEvent.AddOwner(typeof(CalendarDay));

Currently, the Calendar control is the only control that explicitly “knows” what the date is! It has a CurrentDate property.

public static readonly DependencyProperty CurrentDateProperty =
    DependencyProperty.Register("CurrentDate", typeof(DateTime), typeof(Calendar),
        new FrameworkPropertyMetadata((DateTime)DateTime.Now,
            new PropertyChangedCallback(OnCurrentDateChanged)));

The Calendar control also exposes two commands that can be used to change the current date:

public static readonly RoutedCommand NextDay = 
       new RoutedCommand("NextDay", typeof(Calendar));
public static readonly RoutedCommand PreviousDay = 
       new RoutedCommand("PreviousDay", typeof(Calendar));

Commands.jpg

These commands are self explanatory!

I have also created a very basic model:

Model.jpg

This model is used as the data source!

public static readonly DependencyProperty AppointmentsProperty =
    DependencyProperty.Register("Appointments", 
       typeof(IEnumerable<Appointment>), typeof(Calendar),
    new FrameworkPropertyMetadata(null, 
        new PropertyChangedCallback(Calendar.OnAppointmentsChanged)));

The only tricky part now is how to get the data bound to my Appointments property into my ItemsControl?

Introducing filters… Rob Conery’s MVC Storefront uses a similar approach!

public static class Filters 
{ 
    public static IEnumerable<Appointment> ByDate(
           thisIEnumerable<Appointment> appointments, DateTime date) 
    { 
        var app = froma inappointments 
                  wherea.StartTime.Date == date.Date 
                  selecta; 
        return app; 
    } 
}

This extension method allows me to “filter” my appointments by date (ignoring the time). Here is how it is currently used:

private void FilterAppointments()
{
    DateTime byDate = CurrentDate;
    CalendarDay day = this.GetTemplateChild("day") as CalendarDay;
    day.ItemsSource = Appointments.ByDate(byDate);

    TextBlock dayHeader = this.GetTemplateChild("dayHeader") as TextBlock;
    dayHeader.Text = byDate.DayOfWeek.ToString();
}

This approach allows me to relatively easily extend the calendar control to support a day or week view (by just adding multiple CalendarDay controls).

OutlookCalendar.jpg

And, that is it for part 1... If you found this article interesting, please vote for it, and also visit my blog!

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 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
GeneralRoutedEvent question Pinmembermvtongeren22:41 30 Jun '09  
QuestionConveting the Outlook Control to Silverlight PinmemberAnil_gupta7:27 24 May '09  
GeneralCreating an OutLook Calendar using WPF (Part 1) PinmemberMember 26305039:29 30 Dec '08  
GeneralMy vote of 2 PinmemberCharles Perreault8:01 17 Dec '08  
GeneralPragtig Pinmembermwdiablo11:07 28 Oct '08  
GeneralRe: Pragtig Pinmemberrudigrobler1:48 31 Oct '08  
GeneralAdding Appointments Pinmemberdennis oneill22:20 22 Oct '08  
GeneralDate format US? PinmemberPaulLinton18:12 22 Oct '08  
AnswerRe: Date format US? PinmemberPaulLinton18:41 22 Oct '08  
GeneralRe: Date format US? Pinmemberrudigrobler21:04 22 Oct '08  
GeneralFantastic Pinmemberquicoli1:45 22 Oct '08  
GeneralRe: Fantastic Pinmemberrudigrobler1:51 22 Oct '08  
GeneralDate comparisons Pinmembernormanr0:31 22 Oct '08  
GeneralRe: Date comparisons Pinmemberrudigrobler0:40 22 Oct '08  
GeneralVery cool! Pinmemberstrictly8623:38 21 Oct '08  
GeneralRe: Very cool! Pinmemberrudigrobler23:41 21 Oct '08  
GeneralA good start PinmvpSacha Barber23:35 21 Oct '08  
GeneralRe: A good start Pinmemberrudigrobler23:40 21 Oct '08  

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

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