Dispenser/DispenserUI/ViewModels/Converter/StringToDoubleConverter.cs

27 lines
897 B
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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