代码之家  ›  专栏  ›  技术社区  ›  Soleil

如何在xaml中显示具有特定有效小数的货币?

  •  0
  • Soleil  · 技术社区  · 5 年前

    在WPF/xaml代码中,我可以通过以下方式显示一定数量的货币:

    <TextBlock Text="{Binding ., StringFormat={}{0:C}}" xml:lang="fr-FR"/>
    

    680000,00

    68万

    我怎么做?

    0 回复  |  直到 5 年前
        1
  •  1
  •   Pavel Anikhouski    5 年前

    按照你的逻辑,你需要这样的东西

    public class CurrencyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int intValue)
            {
                if (intValue < 1000)
                {
                    return string.Format(culture, "{0:C0}", intValue);
                }
    
                return $"{intValue / 1000} k{culture.NumberFormat.CurrencySymbol}";
            }
    
            return value?.ToString();
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
    

    fr-FR 将文化导入转换器以获得正确的货币符号

    Xaml声明

    <Window.Resources>
            <yournamespace:CurrencyConverter x:Key="currencyConverter"/>
    </Window.Resources>
    

    <TextBlock Text="{Binding ., Converter={StaticResource currencyConverter}, ConverterCulture=fr-FR}"/>
    
        2
  •  2
  •   Corentin Pane    5 年前

    如果你想要68万,你可以试试: StringFormat={}{0:C0}}

    对于680K,以及您想要的所有格式,转换器都能完成任务。