Skip to content

Conversation

@Koichi-Kobayashi
Copy link

@Koichi-Kobayashi Koichi-Kobayashi commented Dec 5, 2025

Pull request type

  • Update
  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes

What is the current behavior?

When MessageBox is displayed, buttons do not receive focus, making keyboard navigation impossible without first clicking a button with the mouse.
(Ver 4.1.0)

Issue Number: N/A

What is the new behavior?

  1. Automatically sets focus to the first available button when MessageBox is displayed
  2. Adds DefaultFocusedButton property to allow developers to specify which button should receive focus
  • Added SetFocusToFirstAvailableButton() method to traverse visual tree and set focus
  • Added DefaultFocusedButton dependency property (nullable MessageBoxButton)
  • Modified OnLoaded() to set focus after visual tree is fully constructed
  • Falls back to automatic selection if specified button is unavailable

Other information

Example

// Automatic focus (default)
var msg = new MessageBox { Title = "Title", Content = "Content" };
await msg.ShowDialogAsync();

// Specify focus target
var msg = new MessageBox 
{ 
    Title = "Title", 
    Content = "Content",
    DefaultFocusedButton = MessageBoxButton.Secondary 
};
await msg.ShowDialogAsync();

- Add SetFocusToFirstAvailableButton method to automatically set focus
- Use Dispatcher.BeginInvoke to ensure visual tree is fully loaded
- Traverse visual tree to find buttons by CommandParameter
- Priority order: Primary > Secondary > Close
…target

- Add DefaultFocusedButton dependency property (MessageBoxButton?)
- Allow users to specify which button should receive focus when MessageBox is displayed
- If DefaultFocusedButton is set, focus is set to the specified button (if available)
- Falls back to automatic selection (Primary > Secondary > Close) if not specified or button is unavailable
@github-actions github-actions bot added controls Changes to the appearance or logic of custom controls. PR Pull request dotnet release labels Dec 5, 2025
@Koichi-Kobayashi Koichi-Kobayashi changed the title Fixes focus issue in MessageBox and adds option to specify focus target. fix(controls): Fixes focus issue in MessageBox and adds option to specify focus target. Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

controls Changes to the appearance or logic of custom controls. dotnet PR Pull request release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant