Click here to Skip to main content
15,885,278 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 154.9K   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;

namespace FsCalendarDemo
{
	public static class DateTimeExtensions
	{
		/// <summary>
		/// Determines whether a subject date is the same as a date passed in.
		/// </summary>
		/// <param name="subjectDate"> The subject date.</param>
		/// <param name="dateToCompare">The date passed in.</param>
		/// <returns>True if the two DateTime objects represent the same date, even if their time components differ; false otherwise.</returns>
		public static bool IsSameDateAs(this DateTime subjectDate, DateTime dateToCompare)
		{
			var dayIsSame = subjectDate.Day == dateToCompare.Day;
			var monthIsSame = subjectDate.Month == dateToCompare.Month;
			var yearIsSame = subjectDate.Year == dateToCompare.Year;
			return dayIsSame && monthIsSame && yearIsSame;
		}

		/// <summary>
		/// Determines whether a subject date is in the same month as a date passed in.
		/// </summary>
		/// <param name="subjectDate"> The subject date.</param>
		/// <param name="dateToCompare">The date passed in.</param>
		/// <returns>True if the two DateTime objects are in the same month; false otherwise.</returns>
		public static bool IsSameMonthAs(this DateTime subjectDate, DateTime dateToCompare)
		{
			var monthIsSame = subjectDate.Month == dateToCompare.Month;
			var yearIsSame = subjectDate.Year == dateToCompare.Year;
			return monthIsSame && yearIsSame;
		}
	}
}

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