You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kev/Drawer/AI/Converters/FormNumberConverter.cs

34 lines
954 B
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Globalization;
using Avalonia.Data.Converters;
namespace AI.Converters
{
/// <summary>
/// 表单数字字段与字符串双向转换NumericUpDown Value 与 CurrentValue 绑定)
/// </summary>
public class FormNumberConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is string s && double.TryParse(s?.Trim(), NumberStyles.Any, culture, out var n))
return n;
return 0d;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is double d)
{
return d.ToString(culture);
}
if (value is decimal dec)
{
return dec.ToString(culture);
}
return "0";
}
}
}