Hello friends, I need to make a string format for cards and I only want to show the last 4 numbers of a binding, but I need to do this from XAML . I know that I can do this from the model class and it would be much easier, but I need to do this from the XAML.
<Label Text="{Binding MaskedMopAccountNumber, StringFormat='???'}" />
Does anyone know how I can do this?
Following the suggested example I have this class:
public class MopDTO : IFormattable
{
public string MaskedMopAccountNumber { get; set; }
public string ExpirationDate { get; set; }
public string ToString(string format, IFormatProvider formatProvider)
{
switch (format)
{
case "Numeros":
return MaskedMopAccountNumber.Substring(MaskedMopAccountNumber.Length - 4);
case "ExpirationDate":
string exp = ExpirationDate;
if (ExpirationDate.Length == 3)
{
exp = ExpirationDate.Insert(1, " / ");
}
else if (ExpirationDate.Length == 4)
{
exp = ExpirationDate.Insert(2, " / ");
}
return exp;
default: return MaskedMopAccountNumber;
}
}
}
and my binding
<Label Padding="10"
Text="{Binding MaskedMopAccountNumber,
StringFormat='{0:Numeros}'}" />
<Label
Padding="10"
HorizontalOptions="EndAndExpand"
Text="{Binding ExpirationDate, StringFormat='{0:ExpirationDate}'}" />
To use the stringFormat in your Model class, example
Cuenta
you must inherit from IFormattableIn your xaml:
your Format in this case will be Numbers. You can debug and see what value enters the switch, if it is null it will go directly to the default. On the other hand, it does not have the property that you want to pass to it, because that value is returned to you by the StringFormat, and you only have to place a .
punto
instead of your property.