0

I have a label inside my listview

<Label Text="{Binding Price,StringFormat='GlobalVariable.Currency{0:F0}'}" 

Result should be : $0.00

I want to concat the currency to the price

My Global Variable Class:

public Static Class GlobalVariable
{
  Currency="$";
}

// currency can be changable.

so how to concat the currency from a class to xaml?

2 Answers 2

2

It is not possible out of the box. See this topic for more info: If I were you, I would either:

  1. format string on ViewModel layer,
  2. use ValueConverter for converting the value.
0
1

You can use a converter:

public class CurrencyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string value = value as string;
        return GlobalVariable + value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
       throw new NotImplementedException();
    }
}

In your Page.xaml

 <ContentPage.Resources>
        <ResourceDictionary>
            <converter:CurrencyConverter x:Key="currencyConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

then, the binding

<Label Text="{Binding Price,Converte={StaticResource currencyConverter}}" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.