private static void HandleDatePickerDialogKey(object sender, DialogKeyEventArgs e) { // If the DatePicker is dropped down and Enter or Escape are pressed, we should // cancel or commit the selection change rather than allowing the default button // or cancel button to be invoked. DatePicker datePicker = (DatePicker)sender; if ((e.Key == Key.Enter || e.Key == Key.Escape) && datePicker.IsDropDownOpen) { e.Handled = true; } }
IntPtr SourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { // Handle WM_GETDLGCODE in order to allow for arrow and tab navigation inside the dialog page. // By returning this code, Windows will pass arrow and tab keys to our HWND instead of handling // them for its own default tab and directional navigation. switch (msg) { case NativeMethods.WM_GETDLGCODE: int dlgCode = NativeMethods.DLGC_WANTARROWS | NativeMethods.DLGC_WANTTAB | NativeMethods.DLGC_WANTCHARS; // Ask the currently-focused element if it wants to handle all keys or not. The DialogKeyPendingEvent // is a routed event starting with the focused control. If any control in the route handles // this message, then we'll add DLGC_WANTALLKEYS to request that this pending message // be delivered to our content instead of the default dialog procedure. IInputElement currentElement = Keyboard.FocusedElement; if (currentElement != null) { DialogKeyEventArgs args = new DialogKeyEventArgs(DialogKeyPendingEvent, KeyInterop.KeyFromVirtualKey(wParam.ToInt32())); currentElement.RaiseEvent(args); if (args.Handled) { dlgCode |= NativeMethods.DLGC_WANTALLKEYS; } } handled = true; return new IntPtr(dlgCode); } return IntPtr.Zero; }