Skip to content

Focus management improvements to ContentDialog#1614

Closed
Koichi-Kobayashi wants to merge 1 commit intolepoco:mainfrom
Koichi-Kobayashi:ContentDialog
Closed

Focus management improvements to ContentDialog#1614
Koichi-Kobayashi wants to merge 1 commit intolepoco:mainfrom
Koichi-Kobayashi:ContentDialog

Conversation

@Koichi-Kobayashi
Copy link
Copy Markdown
Contributor

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?

  • Even when displaying the ContentDialog, focus does not shift to the ContentDialog buttons.
  • Cannot close with the Esc key (though it closes if focus shifts...)
2025-12-18_23h42_12

Issue Number: #1585

What is the new behavior?

  • Add button names (PART_ContentDialogPrimaryButton, PART_ContentDialogSecondaryButton, PART_ContentDialogCloseButton) to XAML template
  • Add Focusable and IsTabStop properties to buttons in XAML template
  • Implement SetFocusToFirstAvailableButton method with checkIsFocused parameter
  • Set focus early in OnLoaded/OnApplyTemplate without validation
  • Validate focus state using Dispatcher.BeginInvoke in OnLoaded to respect developer logic
  • Skip focus setting if already on button or non-button control
  • Auto-focus primaryButton when focus is on ContentDialog or nowhere
  • Add helper methods IsButtonAvailableForFocus and TrySetFocusToButton
  • Add Gallery samples demonstrating focus management behavior:
    • Three Button ContentDialog (Default Focus: Secondary Button)
    • ContentDialog with Focusable Content (Default Focus: Primary Button)
    • ContentDialog with Icons (Default Focus: Secondary Button)
    • ContentDialog with Auto Focus (Default Focus: Primary Button)

Other information

2025-12-18_23h47_56

- Add button names (PART_ContentDialogPrimaryButton, PART_ContentDialogSecondaryButton, PART_ContentDialogCloseButton) to XAML template
- Add Focusable and IsTabStop properties to buttons in XAML template
- Implement SetFocusToFirstAvailableButton method with checkIsFocused parameter
- Set focus early in OnLoaded/OnApplyTemplate without validation
- Validate focus state using Dispatcher.BeginInvoke in OnLoaded to respect developer logic
- Skip focus setting if already on button or non-button control
- Auto-focus primaryButton when focus is on ContentDialog or nowhere
- Add helper methods IsButtonAvailableForFocus and TrySetFocusToButton
- Add Gallery samples demonstrating focus management behavior:
  - Three Button ContentDialog (Default Focus: Secondary Button)
  - ContentDialog with Focusable Content (Default Focus: Primary Button)
  - ContentDialog with Icons (Default Focus: Secondary Button)
  - ContentDialog with Auto Focus (Default Focus: Primary Button)
@github-actions github-actions Bot added controls Changes to the appearance or logic of custom controls. styles Topic is related to styles PR Pull request gallery WPF UI Gallery dotnet release labels Dec 18, 2025
@Koichi-Kobayashi
Copy link
Copy Markdown
Contributor Author

Hello, @apachezy
I also tried fixing the ContentDialog focus issue. What do you think?

@apachezy
Copy link
Copy Markdown
Contributor

@Koichi-Kobayashi

😅, I've reopened #1601. It's possible that it overlaps with this PR...

@Koichi-Kobayashi
Copy link
Copy Markdown
Contributor Author

@apachezy
Oh...
I missed it.

@Koichi-Kobayashi
Copy link
Copy Markdown
Contributor Author

I'm withdrawing this as it was a duplicate.

@apachezy
Copy link
Copy Markdown
Contributor

@Koichi-Kobayashi

I apologize, the discussion I sent you previously was AI-translated and edited, and may be inaccurate or misleading.

The focus priority logic I'm trying to convey is:

  1. The first focusable control in the content area (following the Windows App Design Guidelines; in fact, Windows has had this rule for a long time).

  2. Then try the default button. Since WPFUI buttons can have their "Appearance" property set to "Danger" or "Caution," try avoiding it.

  3. Then try a non-default button that is safe.

  4. Fallback logic...

I only used translation software this time, and I hope my expression is accurate enough.

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 gallery WPF UI Gallery PR Pull request release styles Topic is related to styles

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants