我的listview中有一个标签
<Label Text="{Binding Price,StringFormat='GlobalVariable.Currency{0:F0}'}"
结果应为:0.00美元
我想把货币汇总到价格
我的全局变量类:
public Static Class GlobalVariable
{
Currency="$";
}
//货币可以改变。
那么如何将货币从一个类连接到xaml?
开箱即用是不可能的。有关更多信息,请参阅此topic:如果我是你,我会:
你可以使用转换器:
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();
}
}
在你的Page.xaml中
<ContentPage.Resources>
<ResourceDictionary>
<converter:CurrencyConverter x:Key="currencyConverter" />
</ResourceDictionary>
</ContentPage.Resources>
然后,绑定
<Label Text="{Binding Price,Converte={StaticResource currencyConverter}}" />