Skip to content

UIElement.Focus(FocusState.Programmatic); Returns true despite TextBox not losing focus #11003

@Interceptor2025

Description

@Interceptor2025

Describe the bug

private void TaskItemName_Enter(Microsoft.UI.Xaml.Input.KeyboardAccelerator sender, Microsoft.UI.Xaml.Input.KeyboardAcceleratorInvokedEventArgs args)
  {
      bool f = GridRoot.Focus(FocusState.Programmatic);
      debug3.Text = "FOCUS DEBUG: " + f.ToString();
      args.Handled = true;
  }

Why is this important?

Focus should return true if Focus has changed (i.e. User Presses Enter on a TextBox to change the title).

Steps to reproduce the bug

  1. In MainWIndow.xmal add x:Name to TextBox and an other element.
  2. add the following xmal to TextBox
 <TextBox.KeyboardAccelerators>
   <KeyboardAccelerator Key="Enter" Invoked="TaskItemName_Enter"></KeyboardAccelerator>
 </TextBox.KeyboardAccelerators>
  1. add the following code inside MainWindow.xaml.cs
  private void TaskItemName_Enter(Microsoft.UI.Xaml.Input.KeyboardAccelerator sender, Microsoft.UI.Xaml.Input.KeyboardAcceleratorInvokedEventArgs args)
  {
      //Replace "GridRoot" with the element of your choice.
      bool f = GridRoot.Focus(FocusState.Programmatic);
      //either this or a debugger for the bool f.
      debug3.Text = "FOCUS DEBUG: " + f.ToString();
      args.Handled = true;
  }
  1. select TextBox and press ENTER
  2. get the value of UIElement.Focus()
  3. notice TextBox does not lose focus

Actual behavior

Does not change focus despite returning true.

Expected behavior

Changes focus or returns false.

Screenshots

Image

NuGet package version

WinUI 3 - Windows App SDK 1.8.3: 1.8.251106002

Windows version

Windows 11 (24H2): Build 26100

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-TextBoxTextBox, RichEditBoxbugSomething isn't working

    Type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions