using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Data; namespace BDU.Converters { public class HexConverter : IValueConverter { // 显示时:int → hex 字符串 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is int i) return $"0x{i:X}"; // 例如 255 → 0xFF return "0x0"; } // 用户输入时:hex 字符串 → int public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { var str = value?.ToString()?.Trim(); if (string.IsNullOrWhiteSpace(str)) return 0; if (str.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) str = str.Substring(2); if (int.TryParse(str, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int result)) return result; return 0; // 或 return DependencyProperty.UnsetValue; } } }