Click here to Skip to main content
15,883,904 members
Articles / Desktop Programming / WPF

A couple of tricks when using the standard WPF .NET 4.0 DatePicker control

Rate me:
Please Sign up or sign in to vote.
4.88/5 (48 votes)
9 Mar 2012CPOL6 min read 210.3K   3K   62  
Show hows to alter the DatePicker to use keyboard up/down keys for cleverer date selection.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Markup;

namespace DatePickerTest
{
    /// <summary>
    /// Gets a tool tip for a date passed in. Could also return null
    /// 
    /// Inspired by David Veenemans codeproject article
    /// http://www.codeproject.com/KB/WPF/ExtendingWPFCalendar.aspx
    /// </summary>
        public class HighlightDateConverter : MarkupExtension, IMultiValueConverter
        {
        #region MarkupExtension Overrides
        private static HighlightDateConverter converter = null;
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null)
            {
                converter = new HighlightDateConverter();
            }
            return converter;
        }
        #endregion

        #region IMultiValueConverter Members

        /// <summary>
        /// Gets a tool tip for a date passed in. Could also return null
        /// </summary>
        /// The 'values' array parameter has the following elements:
        /// 
        /// • values[0] = Binding #1: The date to be looked up. This should be set up as a pathless binding; 
        ///   the Calendar control will provide the date.
        /// 
        /// • values[1] = Binding #2: A binding reference to the Calendar control that is invoking this converter.
        /// </remarks>
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // Exit if values not set
            if ((values[0] == null) || (values[1] == null)) return null;

            // Get values passed in
            DateTime targetDate = (DateTime)values[0];
            DatePickerEx dp = (DatePickerEx)values[1];

            var range = dp.BlackoutDates.Where(x => x.Start.IsSameDateAs(targetDate));
            if (range.Count() > 0)
            {
                Dictionary<CalendarDateRange, string> blackOutDatesTextLookup =
                    (Dictionary<CalendarDateRange, string>)dp.GetValue(CalendarProps.BlackOutDatesTextLookupProperty);

                return blackOutDatesTextLookup[range.First()];
            }
            else
                return null;

        }

        /// <summary>
        /// Not used.
        /// </summary>
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            return new object[0];
        }

        #endregion
        }
}

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)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions