27 lines
897 B
C#
27 lines
897 B
C#
using System;
|
||
using System.Globalization;
|
||
using Avalonia.Data.Converters;
|
||
|
||
namespace DispenserUI.ViewModels.Converter;
|
||
|
||
public class StringToDoubleConverter : IValueConverter
|
||
{
|
||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
// 从ViewModel到View的转换逻辑
|
||
if (value is double doubleValue) return doubleValue.ToString();
|
||
|
||
return value.ToString();
|
||
}
|
||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
// 从View到ViewModel的转换逻辑
|
||
if (value is not string stringValue) return value;
|
||
if (double.TryParse(stringValue, out var result)) return result;
|
||
|
||
// 处理无效输入的情况,例如返回0或保留上一有效值等
|
||
// 这里简单处理为返回0,视具体需求调整
|
||
return 0;
|
||
}
|
||
} |