Exemplo n.º 1
0
        private void DateDisplay_LostFocus(object sender, RoutedEventArgs e)
        {
            DateTime result;

            while ((!DateTime.TryParse(DateDisplay.Text, out result) ||
                    (Convert.ToDateTime(DateDisplay.Text) < MinimumDate) ||
                    (Convert.ToDateTime(DateDisplay.Text) > MaximumDate)) &&
                   DateDisplay.CanUndo)
            {
                DateDisplay.Undo();
            }

            if (DateTime.TryParse(DateDisplay.Text, out result) &&
                SelectedDate != Convert.ToDateTime(DateDisplay.Text))
            {
                SelectedDate = Convert.ToDateTime(DateDisplay.Text);
            }
        }
Exemplo n.º 2
0
        private void UserControl_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape || e.Key == Key.Back)
            {
                if (DateDisplay.Text.Length > 0)
                {
                    DateDisplay.Text.Remove(DateDisplay.Text.Length - 1);
                }
                //DateDisplay.Clear();
                return;
            }

            if (e.KeyboardDevice.Modifiers == ModifierKeys.Shift)
            {
                return;
            }

            DateTime dateResult;
            var      selstart = DateDisplay.SelectionStart;

            while (!DateTime.TryParse(DateDisplay.Text, out dateResult))
            {
                DateDisplay.Undo();
            }

            e.Handled = true;
            switch (e.Key)
            {
            case Key.Up:
                SelectedDate = Increase(selstart, 1);
                FocusOnDatePart(selstart);
                break;

            case Key.Down:
                SelectedDate = Increase(selstart, -1);
                FocusOnDatePart(selstart);
                break;

            case Key.Left:
                selstart = SelectPreviousPosition(selstart);
                if (selstart > -1)
                {
                    FocusOnDatePart(selstart);
                }
                else
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous));
                }
                break;

            case Key.Tab:
            case Key.Right:
                selstart = SelectNextPosition(selstart);
                if (selstart > -1)
                {
                    FocusOnDatePart(selstart);
                }
                else
                {
                    PopUpCalendarButton.Focus();
                }
                break;

            default:
                try
                {
                    if (!Char.IsDigit(Convert.ToChar(e.KeyboardDevice.ToString())))
                    {
                        if (e.Key == Key.D0 ||
                            e.Key == Key.D1 ||
                            e.Key == Key.D2 ||
                            e.Key == Key.D3 ||
                            e.Key == Key.D4 ||
                            e.Key == Key.D5 ||
                            e.Key == Key.D6 ||
                            e.Key == Key.D7 ||
                            e.Key == Key.D8 ||
                            e.Key == Key.D9)
                        {
                            e.Handled = false;
                        }
                    }
                    break;
                }
                catch { break; }
            }
        }