locked
No GotFocus event for WPF DatePickerTextBox? RRS feed

  • Question

  • Whether you tab into or mouse click into the WPF DatePicker TextBox area, you never get a GotFocus event. You do get one if you click on the CalendarButton, but even when you select a date and start typing, you still never get a GotFocus event on the TextBox. You do get a GotMouseCapture event, but I would have expected a GotFocus event as the TextBox clearly has acquired keyboard focus. Is this a bug?
    Tuesday, August 31, 2010 2:32 AM

Answers

  • Hi Nigel,

    DatePicker handles it's GotFocus event internally. And it changes e.Handled to true for all GotFocus RoutedEvents triggered by its textbox.

    DatePicker.GotKeyboardFocus event is not effected by this. You can listen to that event instead.

    If you do need GotFocus event to fire, you can explicitly add an event handler to listen to this handled GotFocus event and change e.Handled to false.

    Here is an example:

    DatePicker1.AddHandler(DatePicker.GotFocusEvent, new RoutedEventHandler(DatePicker1_GotFocus), true);
    
      void DatePicker1_GotFocus(object sender, RoutedEventArgs e)
      {
       e.Handled = false;
      }
    

    If this DatePicker is in a template, you can still add this handler in it's loaded event.

    Here is an example:

    <DatePicker Loaded="DatePicker_Loaded"/>
    
      private void DatePicker_Loaded(object sender, RoutedEventArgs e)
      {
       (sender as DatePicker).AddHandler(DatePicker.GotFocusEvent, new RoutedEventHandler(DatePicker_GotFocus), true);
      }
      void DatePicker_GotFocus(object sender, RoutedEventArgs e)
      {
       e.Handled = false;
      }
    

    Hope this helps.

    If you still have any doubts or concerns about this issue, please feel free to let me know.

    Best regards,

    Min 


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Wednesday, September 8, 2010 1:16 AM
    Friday, September 3, 2010 5:47 AM
  • <my:DatePicker Grid.Row="1" Margin="12,13.361,107,13.639" Name="datePicker1" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" GotKeyboardFocus= "datePicker1_GotKeyboardFocus" />

     

     

    GotKeyboardFocus event is working on focus of the datetimepicker

     

      private void datePicker1_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)

            {

                datePicker1.DataContext = "1/1/2010";

            }

     

    Tuesday, August 31, 2010 4:45 PM

All replies

  • <my:DatePicker Grid.Row="1" Margin="12,13.361,107,13.639" Name="datePicker1" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" GotKeyboardFocus= "datePicker1_GotKeyboardFocus" />

     

     

    GotKeyboardFocus event is working on focus of the datetimepicker

     

      private void datePicker1_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)

            {

                datePicker1.DataContext = "1/1/2010";

            }

     

    Tuesday, August 31, 2010 4:45 PM
  • I'm not using the toolkit version, I'm using .NET4 so it's in the framework now. Also, it's GotFocus that doesn't work, not GotKeyboardFocus.
    Tuesday, August 31, 2010 8:10 PM
  • Oh I am not using VS 2010
    Tuesday, August 31, 2010 8:16 PM
  • Hi Nigel,

    DatePicker handles it's GotFocus event internally. And it changes e.Handled to true for all GotFocus RoutedEvents triggered by its textbox.

    DatePicker.GotKeyboardFocus event is not effected by this. You can listen to that event instead.

    If you do need GotFocus event to fire, you can explicitly add an event handler to listen to this handled GotFocus event and change e.Handled to false.

    Here is an example:

    DatePicker1.AddHandler(DatePicker.GotFocusEvent, new RoutedEventHandler(DatePicker1_GotFocus), true);
    
      void DatePicker1_GotFocus(object sender, RoutedEventArgs e)
      {
       e.Handled = false;
      }
    

    If this DatePicker is in a template, you can still add this handler in it's loaded event.

    Here is an example:

    <DatePicker Loaded="DatePicker_Loaded"/>
    
      private void DatePicker_Loaded(object sender, RoutedEventArgs e)
      {
       (sender as DatePicker).AddHandler(DatePicker.GotFocusEvent, new RoutedEventHandler(DatePicker_GotFocus), true);
      }
      void DatePicker_GotFocus(object sender, RoutedEventArgs e)
      {
       e.Handled = false;
      }
    

    Hope this helps.

    If you still have any doubts or concerns about this issue, please feel free to let me know.

    Best regards,

    Min 


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Wednesday, September 8, 2010 1:16 AM
    Friday, September 3, 2010 5:47 AM
  • This behaviour makes it different from almost all other controls, as far as I'm aware, and different even within the control itself, as clicking on the calendar button does fire the GotFocus event! Whilst I appreciate the workaround (which I'm already using) I really think that it should behave in a consistent manner with respect to itself and other controls.

    Wednesday, September 8, 2010 1:20 AM