protected override void OnDropDownOpened(EventArgs e) { base.OnDropDownOpened(e); PART_PopupListBox.Focus(); if (PART_PopupListBox.Items.Count == 0) { return; } var index = PART_PopupListBox.SelectedIndex; if (index < 0) { index = 0; } Action action = () => { PART_PopupListBox.ScrollIntoView(PART_PopupListBox.SelectedItem); if (PART_PopupListBox.ItemContainerGenerator.ContainerFromIndex(index) is ListBoxItem item) { item.Focus(); KeyboardNavigationEx.Focus(item); } }; Dispatcher.BeginInvoke(DispatcherPriority.Background, action); }
private void setup(Window owner, string messageText, string title, MessageBoxButton buttons, string okButtonText) { this.Owner = owner; this.messageTextBlock.Text = messageText; this.titleTextBlock.Text = title; var ok = buttons == MessageBoxButton.OK || buttons == MessageBoxButton.OKCancel; var yesno = buttons == MessageBoxButton.YesNo || buttons == MessageBoxButton.YesNoCancel; var cancel = buttons == MessageBoxButton.OKCancel || buttons == MessageBoxButton.YesNoCancel; this.okButton.ShowOnlyIf(ok); this.yesButton.ShowOnlyIf(yesno); this.noButton.ShowOnlyIf(yesno); this.cancelButton.ShowOnlyIf(cancel); this.okButton.Content = okButtonText; KeyboardNavigationEx.Focus(ok ? okButton : yesButton); if (owner != null) { this.Topmost = owner.Topmost; } }
public MainWindow() { InitializeComponent(); this.Loaded += (sender, args) => { KeyboardNavigationEx.Focus(this.firstButton); }; }
public static void SetFocus(this IViewAware screen, string property) { if (!(screen.GetView() is DependencyObject view)) { return; } FrameworkElement control = FindChild(view, property); if (control == null) { return; } KeyboardNavigationEx.Focus(control); }
private void onIdleNotification(string guid, string since, string duration, long started, string description) { if (this.TryBeginInvoke(this.onIdleNotification, guid, since, duration, started, description)) { return; } this.guid = guid; this.started = started; this.timeText.Text = since; this.durationText.Text = duration; this.descriptionText.Text = string.IsNullOrEmpty(description) ? "No description" : description; this.Show(); this.Topmost = true; this.Activate(); KeyboardNavigationEx.Focus(this.keepTimeButton); }
internal Task <MessageDialogResult> WaitForButtonPressAsync() { this.Dispatcher.BeginInvoke(new Action(() => { this.Focus(); var defaultButtonFocus = this.DialogSettings.DefaultButtonFocus; //Ensure it's a valid option if (!this.IsApplicable(defaultButtonFocus)) { defaultButtonFocus = this.ButtonStyle == MessageDialogStyle.Affirmative ? MessageDialogResult.Affirmative : MessageDialogResult.Negative; } //kind of acts like a selective 'IsDefault' mechanism. switch (defaultButtonFocus) { case MessageDialogResult.Affirmative: this.PART_AffirmativeButton.SetResourceReference(StyleProperty, "AccentedDialogSquareButton"); KeyboardNavigationEx.Focus(this.PART_AffirmativeButton); break; case MessageDialogResult.Negative: this.PART_NegativeButton.SetResourceReference(StyleProperty, "AccentedDialogSquareButton"); KeyboardNavigationEx.Focus(this.PART_NegativeButton); break; case MessageDialogResult.FirstAuxiliary: this.PART_FirstAuxiliaryButton.SetResourceReference(StyleProperty, "AccentedDialogSquareButton"); KeyboardNavigationEx.Focus(this.PART_FirstAuxiliaryButton); break; case MessageDialogResult.SecondAuxiliary: this.PART_SecondAuxiliaryButton.SetResourceReference(StyleProperty, "AccentedDialogSquareButton"); KeyboardNavigationEx.Focus(this.PART_SecondAuxiliaryButton); break; } })); TaskCompletionSource <MessageDialogResult> tcs = new TaskCompletionSource <MessageDialogResult>(); RoutedEventHandler negativeHandler = null; KeyEventHandler negativeKeyHandler = null; RoutedEventHandler affirmativeHandler = null; KeyEventHandler affirmativeKeyHandler = null; RoutedEventHandler firstAuxHandler = null; KeyEventHandler firstAuxKeyHandler = null; RoutedEventHandler secondAuxHandler = null; KeyEventHandler secondAuxKeyHandler = null; KeyEventHandler escapeKeyHandler = null; Action cleanUpHandlers = null; var cancellationTokenRegistration = this.DialogSettings.CancellationToken.Register(() => { cleanUpHandlers?.Invoke(); tcs.TrySetResult(this.ButtonStyle == MessageDialogStyle.Affirmative ? MessageDialogResult.Affirmative : MessageDialogResult.Negative); }); cleanUpHandlers = () => { this.PART_NegativeButton.Click -= negativeHandler; this.PART_AffirmativeButton.Click -= affirmativeHandler; this.PART_FirstAuxiliaryButton.Click -= firstAuxHandler; this.PART_SecondAuxiliaryButton.Click -= secondAuxHandler; this.PART_NegativeButton.KeyDown -= negativeKeyHandler; this.PART_AffirmativeButton.KeyDown -= affirmativeKeyHandler; this.PART_FirstAuxiliaryButton.KeyDown -= firstAuxKeyHandler; this.PART_SecondAuxiliaryButton.KeyDown -= secondAuxKeyHandler; this.KeyDown -= escapeKeyHandler; cancellationTokenRegistration.Dispose(); }; negativeKeyHandler = (sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Negative); } }; affirmativeKeyHandler = (sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Affirmative); } }; firstAuxKeyHandler = (sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.FirstAuxiliary); } }; secondAuxKeyHandler = (sender, e) => { if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.SecondAuxiliary); } }; negativeHandler = (sender, e) => { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Negative); e.Handled = true; }; affirmativeHandler = (sender, e) => { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Affirmative); e.Handled = true; }; firstAuxHandler = (sender, e) => { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.FirstAuxiliary); e.Handled = true; }; secondAuxHandler = (sender, e) => { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.SecondAuxiliary); e.Handled = true; }; escapeKeyHandler = (sender, e) => { if (e.Key == Key.Escape) { cleanUpHandlers(); tcs.TrySetResult(this.ButtonStyle == MessageDialogStyle.Affirmative ? MessageDialogResult.Affirmative : MessageDialogResult.Negative); } else if (e.Key == Key.Enter) { cleanUpHandlers(); tcs.TrySetResult(MessageDialogResult.Affirmative); } }; this.PART_NegativeButton.KeyDown += negativeKeyHandler; this.PART_AffirmativeButton.KeyDown += affirmativeKeyHandler; this.PART_FirstAuxiliaryButton.KeyDown += firstAuxKeyHandler; this.PART_SecondAuxiliaryButton.KeyDown += secondAuxKeyHandler; this.PART_NegativeButton.Click += negativeHandler; this.PART_AffirmativeButton.Click += affirmativeHandler; this.PART_FirstAuxiliaryButton.Click += firstAuxHandler; this.PART_SecondAuxiliaryButton.Click += secondAuxHandler; this.KeyDown += escapeKeyHandler; return(tcs.Task); }
public MainWindow() { InitializeComponent(); Loaded += (s, e) => { KeyboardNavigationEx.Focus(TxtBruto); }; }