WPF: Focus DatePicker on Text in TextBox


WPF: Focus DatePicker on Text in TextBox

I’ve been using the DatePicker control (which can be found in WPF 4.0 as well as the WPF Toolkit) and have come across a situation where I would like to set keyboard focus on a DatePicker programmatically so that the user can immediately begin typing a date into the control. Well like many other people out there I attempted to do this by calling the controls Focus() method and when that didn’t have the desired results I tried Keyboard.Focus() and passed it the DatePicker. This also didn’t accomplish what I was looking for.

In the above cases the DatePicker would indeed gain focus, however you could not immediately begin typing in a date. If you pressed a navigational key on the keyboard such as Tab or any of the directional arrows it would then put the keyboard focus into the TextBox portion of the control and highlight any existing text.

DatePicker without focus on text

Focus, but not on text…

DatePicker with focus on text

Focus on text!

Since this is exactly the end result I was after I set about automating this in code and arrived at the following solution. Basically all I’m doing is simulating the Up Arrow being pressed on the DatePicker control, this causes the control to move focus to the internal TextBox. It’s very hacky but it’s the best thing I could come up with without inheriting from DatePicker which I’d like to avoid. I wrapped the code up in a nice extension method for ease of use, and that’s what you see below.

using System.Windows.Controls;
using System.Windows.Input;
namespace CGS
    public static class DatePickerExtensions
        public static void FocusOnText(this DatePicker datePicker)
            var eventArgs = new KeyEventArgs(Keyboard.PrimaryDevice,
            eventArgs.RoutedEvent = DatePicker.KeyDownEvent;

If anyone knows of a cleaner, more legitimate way to accomplish this please let me know! As always questions and comments are welcome and appreciated.

About the author

Mel administrator

4 Comments so far

HARRYPosted on6:02 pm - Oct 10, 2016

I’m new to programming. I created namespace and add using into it but it is not selecting the text nor getting focus either. (No Err msg)

(VS Express 2015. WPF.)


Appreciate any Help !!!

    HARRYPosted on12:41 pm - Oct 11, 2016

    eventArgs.RoutedEvent = DatePicker.GotFocusEvent; //.KeyDownEvent;

    Fixed it !!!

    I changed the event from KeyDownEvent to GotFocusEvent !!! in Window_Loaded event.

VarunPosted on1:35 am - Aug 6, 2012

This solved my problem..

Kunwar KhaanPosted on7:09 am - Mar 2, 2012

This is one of the best answer so far, I have read online. Just useful information. Very well presented. You made it very simple and understandable. I’ve found some other good articles on DatePicker control in WPF which also explained very well. Here I’m sharing the link of that helpful post…..
Thanks everybody.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.