Skip to content

Conversation

@Foda
Copy link
Member

@Foda Foda commented Jun 27, 2024

Description of Change

Consider that you want to implement a "select all text on focus" function for an Entry field:

private void OnEntryFocused(object? sender, FocusEventArgs e)
{
    if (TextBox.Text != null)
    {
        TextBox.CursorPosition = 0;
        TextBox.SelectionLength = entry.Text.Length;
    }
}

Currently, this will work the first time the Entry is focused, but it won't work the second time.

The problem is caused by the following:

  • Entry gains focus
  • Cursor position and selection length are set, values propagate to WinUI TextBox
  • Entry is unfocused
  • Entry gains focus again
  • Cursor position and selection length are set, but the values are the same, so we don't set the values again on the WinUI TextBox
  • WinUI TextBox handles input event, moves cursor to where you click (note: the native WinUI TextBox seems to have special logic around canceling this logic if setting selection during an OnFocus event? this is where the issue comes from!)
  • WinUI control then propagates cursor position and selection length values to the Maui Entry, text is now unselected

The fix here is to just always ensure setting cursor position and selection length flows to the native WinUI TextBox control. A better solution might be adding a Select and SelectAll method to Entry, but for now this will work.

Mike Corsaro added 2 commits June 27, 2024 10:42
…ts even if the value is the same

This allows "select all text on focus" logic to work on WinUI
@Foda Foda requested a review from PureWeen June 27, 2024 22:19
@Foda Foda requested a review from a team as a code owner June 27, 2024 22:19
@Foda Foda requested a review from Eilon June 27, 2024 22:19
{
get { return (int)GetValue(CursorPositionProperty); }
set { SetValue(CursorPositionProperty, value); }
set
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@StephaneDelcroix thoughts on this?

Copy link
Member

@PureWeen PureWeen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better solution might be adding a Select and SelectAll method to Entry, but for now this will work.

We could just add this for now via the command mappers. Would that be a better solution here then firing the UpdateValue everytime?

Now in net9 with the new overrides, we could just target this to net9 and then fire a commandmapper that does the "SelectAll" behavior.

@jsuarezruiz
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@Foda
Copy link
Member Author

Foda commented Nov 18, 2024

/rebase

@Foda
Copy link
Member Author

Foda commented Mar 12, 2025

/rebase

@TrainCo
Copy link

TrainCo commented Dec 22, 2025

Has this code been merged? The issue is pretty old but we still have a pending issue open that references this fix and from what I see on this page, the PR has been "running" since January so I just want to be sure someone has this in their sights.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants