Space 作为标签的千位分隔符

Space as thousands separator for labels

我有一个标签,我正在使用绑定和格式字符串:

<Label Text="{Binding buying_price , StringFormat='{0} EUR'}">

标签的文本在我的 ViewModel 中绑定到双精度,我在标签上得到的是这样的:10000 EUR,我想要得到的是 10 000 EUR,[=14] =] 例如( 没有尾随 .00)。 我尝试了 StringFormat='{0:C2} EUR'StringFormat='{0:N} EUR' 以及 StringFormat='{0:n} EUR' 但没有一个给了我一个好的结果。

根据您的文化尝试使用 StringFormat='{}{0:#,##,#}' 如果您得到 10,000 EUR 而不是 10 000 EUR 的结果,您可能需要在 code-behind 中更改它以前 , 是千位分隔符而不是逗号。

您可能想查看可用的完整文档 numeric format string

对文化有帮助的相关问题:How would I separate thousands with space in C#

我没有让它在 xaml 中工作,而当我在代码后面使用转换和使用字符串格式时,它正常工作:

<ContentPage.Resources>
    <ResourceDictionary>
        <local:thousandsSeparatorConverter x:Key="thousandsSeparator"/>
    </ResourceDictionary>
</ContentPage.Resources>

<StackLayout>
    <!-- Place new controls here -->
    <Label Text="{Binding date, Converter={StaticResource thousandsSeparator}}"  HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
    
</StackLayout>

还有 local:thousandsSeparatorConverter :

public class thousandsSeparatorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string s = value as string;


        double number = double.Parse(s);

        // Gets a NumberFormatInfo associated with the en-US culture.
        NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

        // Displays the same value with a blank as the separator.
        nfi.NumberGroupSeparator = " ";
        Console.WriteLine(number.ToString("N0", nfi));

        string convertedNumber = number.ToString("N0", nfi);

        return convertedNumber;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }

}