简体   繁体   中英

Decimal point is being removed in Entry

My converter is the following

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;
    }
}

When I enter a number

55 号

Next entered the separator

另存为 55

When I enter the decimal

在此处输入图像描述

ends as 55

In the view before

图片

after

在此处输入图像描述

Resource:

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

Entry defined in the XAML

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

How could I solve it if I want it to be according to the culture?

You can enforce "en-us" culture for TryParse()

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;
    }
}

Alternative approach

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM