Xamarin adding string to entry in mvvm

Viewed 50

I have an entry which I want the user to input digits and that there will always be a "%" in the end. I prefer not to have an additional label next to the entry. So far I tried to format the entry and add the % to the value but the result is that after every digit the symbol is added. My failed attemts:

First solution attempt in Xaml:

<Entry Text="{Binding Test, StringFormat='{0:F0}%'}"/>

Second solution attempt in View-Model:

string _test;
public string Test
        {
            get => _test;
            set
            {
                value += "%";
                SetProperty(ref _test, value);
            }
        }

In both cases, if the user's input was 56 the entry showed 5%6%. I understand why it happens but I can't think of a solution. Any ideas?

1 Answers

Check to see if the value has % character

string _test;
public string Test
{
   get => _test;
   set
   {
      if(value.Contains("%"))
         value = value.Replace("%","");
      value += "%";
      SetProperty(ref _test, value);
   }
}
Related