xamarin 正在删除条目中的小数点

wribegjk  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(124)

我的转换器如下

public class DecimalConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is decimal)
                   return value.ToString ();
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
       decimal dec;
       if (decimal.TryParse(value as string, out dec))
           return dec;
       else
           return 0;
    }
}

输入数字时

接下来进入分隔符

输入小数时

结尾为55
在观前

之后


指令集
资源:

<support:DecimalConverter x:Key="DecimalConverter" />

在XAML中定义的条目

<Entry
            BackgroundColor="{TemplateBinding BackgroundColorAmountEntry}"
            IsEnabled="{TemplateBinding IsByAmount}" Keyboard="Numeric"
            Text="{TemplateBinding CommissionWithTaxAmount,
            Converter={StaticResource DecimalConverter}}" />

如果我想让它符合文化,我该如何解决它呢?

uqcuzwp8

uqcuzwp81#

您可以对TryParse()胁迫执行“en-us”文化特性

public class DecimalConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is decimal)
                   return value.ToString ();
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
       decimal dec;
        var style = NumberStyles.Number | NumberStyles.AllowDecimalPoint;
        var cultureEnUs = new CultureInfo("en-US");
        if (decimal.TryParse(value as string, style, cultureEnUs, out dec))
           return dec;
       else
           return 0;
    }
}

替代方法

When language is set to French, decimal point separator ',' is ignored in binding to a double property

相关问题