microsoft / microsoft/microsoft-ui-xaml
UIElement.Focus(FocusState.Programmatic); Returns true despite TextBox not losing focus
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 8.4k
- Forks
- 942
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 105
Description
### 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
```
```
3. 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;
}
```
4. select TextBox and press ENTER
5. get the value of UIElement.Focus()
6. notice TextBox does not lose focus
### Actual behavior
Does not change focus despite returning true.
### Expected behavior
Changes focus or returns false.
### Screenshots
### 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_
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue from MainWindow.xaml and MainWindow.xaml.cs using the provided KeyboardAccelerator and TaskItemName_Enter handler. Start by checking the return value and resulting focus after selecting the TextBox and pressing Enter; done means focus changes as expected or Focus returns false when it does not.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100