Syncfusion DoubleTextBox 转换来自用户的输入
Syncfusion DoubleTextBox Convert Input from User
我们遇到了目前正在处理的问题。
我们想使用 syncfusion 中的 DoubleTextBox 或类似的 WPF 元素。
问题是:
用户应该可以在字段中输入 345,它会自动更正为 3.45
如果他输入 35,它应该是 0.35
如果他输入 4.56,它应该是 4.56
到目前为止,我们为绑定实现了一个转换器,它做得很好。
但是,如果该值是通过数据库输入的十进制值,如 300,即 300.00
转换器寻找点“。” -> 没有找到它并放置一个所以 300 现在是 3.00
这是错误的。
如果数据库值为 312.45,则它运行良好。小数点截去所有零位:/
我们目前无法为此使用我们的转换器。
有没有人对我们的问题有想法? syncfusion 中是否有 WPF 元素可以做到这一点?
这是转换器:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
if (targetType != typeof(object))
{
// Formatierung bei Eingabefeldern
if (value != null)
{
string result = String.Empty;
result = value.ToString();
if (!value.ToString().Contains(","))
{
decimal _formatted = System.Convert.ToDecimal(value) / 100;
result = _formatted.ToString("F");
}
else if (value.ToString().Contains(","))
{
decimal _formatted = System.Convert.ToDecimal(value);
result = string.Format("{0:F2}", _formatted);
}
return result.ToString();
}
}
else
{
// Formatierung bei nicht Eingabefeldern
if (value == String.Empty)
{
value = 0;
}
decimal _formattedcomputed = System.Convert.ToDecimal(value);
string resultcomputed = string.Format("{0:F2}", _formattedcomputed);
return resultcomputed;
}
}
catch (Exception ex)
{
}
return null;
}
这对我来说工作正常。
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
// Formatierung bei Eingabefeldern
if (value != null)
{
string inputStr = value.ToString();
decimal inputDecimal = System.Convert.ToDecimal(value);
if (inputStr.Contains(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator))
{
return inputDecimal.ToString("F2");
}
else
{
inputDecimal /= 100;
return inputDecimal.ToString("F");
}
}
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(
String.Format("Error converting value {0}: {1}", value, ex.Message));
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
我们遇到了目前正在处理的问题。 我们想使用 syncfusion 中的 DoubleTextBox 或类似的 WPF 元素。 问题是:
用户应该可以在字段中输入 345,它会自动更正为 3.45 如果他输入 35,它应该是 0.35 如果他输入 4.56,它应该是 4.56 到目前为止,我们为绑定实现了一个转换器,它做得很好。 但是,如果该值是通过数据库输入的十进制值,如 300,即 300.00 转换器寻找点“。” -> 没有找到它并放置一个所以 300 现在是 3.00 这是错误的。 如果数据库值为 312.45,则它运行良好。小数点截去所有零位:/
我们目前无法为此使用我们的转换器。
有没有人对我们的问题有想法? syncfusion 中是否有 WPF 元素可以做到这一点?
这是转换器:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
if (targetType != typeof(object))
{
// Formatierung bei Eingabefeldern
if (value != null)
{
string result = String.Empty;
result = value.ToString();
if (!value.ToString().Contains(","))
{
decimal _formatted = System.Convert.ToDecimal(value) / 100;
result = _formatted.ToString("F");
}
else if (value.ToString().Contains(","))
{
decimal _formatted = System.Convert.ToDecimal(value);
result = string.Format("{0:F2}", _formatted);
}
return result.ToString();
}
}
else
{
// Formatierung bei nicht Eingabefeldern
if (value == String.Empty)
{
value = 0;
}
decimal _formattedcomputed = System.Convert.ToDecimal(value);
string resultcomputed = string.Format("{0:F2}", _formattedcomputed);
return resultcomputed;
}
}
catch (Exception ex)
{
}
return null;
}
这对我来说工作正常。
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
// Formatierung bei Eingabefeldern
if (value != null)
{
string inputStr = value.ToString();
decimal inputDecimal = System.Convert.ToDecimal(value);
if (inputStr.Contains(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator))
{
return inputDecimal.ToString("F2");
}
else
{
inputDecimal /= 100;
return inputDecimal.ToString("F");
}
}
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(
String.Format("Error converting value {0}: {1}", value, ex.Message));
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}