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
Next entered the separator
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;
}
}
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.