CommunityToolkit/Maui.Markup

[Bug] Cursor Position wrong in Entry field when using IValueConverter

devonuto opened this issue · 5 comments

I have a page created with a number of entry fields which are formatted as either dollars, or percentages. When editing the value, once the new string has been formatted and returned to the field, the caret switches to the front of the entry field, making it unusable. See repo/gif at end

Stack Trace

N/A

Link to Reproduction Sample

https://github.com/devonuto/MauiBugs/tree/master/FormattingBug

Steps to Reproduce

  1. Add Entry field to page
  2. Bind entry field to value and converter
  3. Alter the value in the entry field

Expected Behavior

The cursor/caret would stay at the end of the entry field and not move to the start

Actual Behavior

It moves to ahead of the dollar sign after each keypress. This didn't happen using the same converter and XAML in Xamarin forms.

Basic Information

  • Version with issue: RC2
  • Last known good version: Unknown
  • IDE:
  • Platform Target Frameworks:
    • Android: 12.1 (API 32)
  • Android Support Library Version:
  • Nuget Packages:
    <PackageReference Include="CommunityToolkit.Maui.Markup" Version="1.0.0-rc2" /
- Affected Devices:

Workaround

Tried to do this, but it wasn't reliable enough

private void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
    var entry = sender as Entry;
    entry.CursorPosition = entry.Text.Length;
}

Reproduction imagery

qemu-system-x86_64_6iH86GbTfO

Hi @devonuto

I am not sure this is an issue with the CommunityToolkit.Maui.Markup, if anything I would expect it to be raised against the .NET MAUI repository instead. However, I am not convinced this is a bug...

Currently you have implemented Entry.TextProperty bound to MainPageViewModel.EntryValue and this will produce the following behaviour:

  • User enters a value in the Entry
  • MainPageViewModel.EntryValue receives the value from Entry.Text but converted with the DecimalCurrencyConverter which is different to the value in the Entry.
  • Entry then receives the updated value from the view model. I suspect this last step is messing with the caret position because the value is being replaced.

I am not convinced you really want this last behaviour.

In order to achieve the behaviour that you need you may wish to look at implementing a Behavior. The MaskedBehavior from our other toolkit repo may helped for inspiration here https://github.com/CommunityToolkit/Maui/blob/main/src/CommunityToolkit.Maui/Behaviors/MaskedBehavior.shared.cs

I raised it as a bug as it worked in Xamarin in the same method which I found in another app. And raised it here because it's creating the page with code, not xaml. But i will look at the maakedbehavior demo, thanks.

I raised it as a bug as it worked in Xamarin in the same method which I found in another app. And raised it here because it's creating the page with code, not xaml. But i will look at the maakedbehavior demo, thanks.

Hmmm ok if it worked on Xamarin then this may be a regression and that is something the .NET MAUI team should know more on.

This toolkit only makes it easier to create the .NET MAUI controls, the implementation is still provided by them. This is their repo: https://github.com/dotnet/maui

Thanks, raised it over there too

OK thanks. I will close this down for now but feel free to reach out if needed.

.NET MAUI issue:
dotnet/maui#6948