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.
Sign up to request clarification or add additional context in comments.

Comments

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}}" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.