Click here to Skip to main content
15,897,226 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 156K   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.IO;
using System.Reflection;
using System.Windows;
using System;
using FsControls;

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

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

		   var fileNames = new string[2];
		   fileNames[0] = String.Format(@"{0}\Holidays1.txt", appFolderPath);
		   fileNames[1] = String.Format(@"{0}\Holidays2.txt", appFolderPath);

		   var calendars = new FsCalendar[2];
		   calendars[0] = this.DemoCalendar1;
		   calendars[1] = this.DemoCalendar2;


		   for (var i = 0; i < 2; i++)
		   {
			   using (var reader = new StreamReader(fileNames[i]))
			   {
				   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
					   calendars[i].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