Forum Xamarin.Forms

Bug in Entry with StringFormat and commas

JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
edited May 2018 in Xamarin.Forms

I've come across a bug in the Entry field, related to how it handles commas in a string format.

With the following XAML:

    Text       ="{Binding Mileage , StringFormat='{0:N0}'}"

    Keyboard   ="Numeric"
    Text       ="{Binding Mileage , StringFormat='{0:N0}'}"
    Placeholder="Enter Mileage"

With the bound Mileage property defined in the ViewModel as:

private int _mileage;
public int Mileage
        return _mileage;
        _mileage = value;

We should see commas being inserted into the Entry control as we type, and for a sanity check, I've got the label showing the bound value, again formatted.

With just two digits - all is good:

With 4 digits - looking good still!

But as soon as we type another digit with the comma already in the Entry, the backing property isn't updated (note that the '5' in the Entry hasn't been added to the Label):

I think the reason is that the comma in the Entry is causing the automatic parse from string to int to fail. If we edit out the commas in the Entry field, then it works:

Note the comma in the Entry is re-inserted as part of the format processing.

There is already this issue in Git

Entries bound to number fields don't handle cultures that use a comma as a fraction separator

but I think that's more related to cultures rather than a core parsing bug.

Unless others can point me to an existing Git issue, I think I should raise this as a bug.

The next question is - what's the workaround/fix for this? I suppose I'm going to have to write a custom renderer...

This is with Forms version


Sign In or Register to comment.