Click here to Skip to main content
15,891,033 members
Articles / Desktop Programming / WPF

Extending the WPF Calendar Control

Rate me:
Please Sign up or sign in to vote.
4.84/5 (28 votes)
10 Sep 2010CPOL26 min read 155.5K   5K   65  
This article shows how to create a custom control that extends an existing WPF control. It extends the WPF Calendar control by adding date highlighting.
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows;
using System;

namespace FsCalendarDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadCalendar();
        }

        /// <summary>
        /// Loads highlighted dates from a text file into a FsCalendar control.
        /// </summary>
        private void LoadCalendar()
        {
            // Get folder containing this app
            var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var n = path.LastIndexOf(@"\bin");
            var appFolderPath = path.Remove(n);

            var fileName = String.Format(@"{0}\Holidays.txt", appFolderPath);

            using (var reader = new StreamReader(fileName))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    // Parse line into data and tool tip
                    var s = line.Split(';');
                    var dateString = s[0];
                    var toolTip = s[1];

                    // Create DateTime object for date
                    var d = dateString.Split('/');
                    var date = new DateTime(DateTime.Now.Year, Convert.ToInt32(d[0]), Convert.ToInt32(d[1]));

                    // Add item to Calendar control's highlighted dates
                    DemoCalendar.HighlightedDates.Add(date, toolTip);
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) Foresight Systems
United States United States
David Veeneman is a financial planner and software developer. He is the author of "The Fortune in Your Future" (McGraw-Hill 1998). His company, Foresight Systems, develops planning and financial software.

Comments and Discussions