using System.Globalization; using System.Windows; using System.Windows.Data; using System.Windows.Media; using SSPCTester.Logic.Models; using SSPCTester.UI.Services; namespace SSPCTester.UI.Converters; /// TestStatus → 实色徽标背景。 public sealed class StatusToBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object? parameter, CultureInfo culture) { var key = value switch { true => "SuccessBrush", false => "DangerBrush", TestStatus.Pass => "SuccessBrush", TestStatus.Fail => "DangerBrush", TestStatus.Running => "RunningBrush", TestStatus.Measured => "PrimaryBrush", _ => "NeutralBrush" }; return Application.Current.TryFindResource(key) as Brush ?? Brushes.Gray; } public object ConvertBack(object value, Type targetType, object? parameter, CultureInfo culture) => Binding.DoNothing; } /// TestStatus → 浅色徽标背景。 public sealed class StatusToSoftBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object? parameter, CultureInfo culture) { var key = value switch { true => "SuccessSoftBrush", false => "DangerSoftBrush", TestStatus.Pass => "SuccessSoftBrush", TestStatus.Fail => "DangerSoftBrush", TestStatus.Running => "RunningSoftBrush", TestStatus.Measured => "PrimarySoftBrush", _ => "NeutralSoftBrush" }; return Application.Current.TryFindResource(key) as Brush ?? Brushes.LightGray; } public object ConvertBack(object value, Type targetType, object? parameter, CultureInfo culture) => Binding.DoNothing; } /// TestStatus → 中文文本。 public sealed class StatusToTextConverter : IValueConverter { public object Convert(object value, Type targetType, object? parameter, CultureInfo culture) => value is TestStatus s ? s switch { TestStatus.Pass => "合格", TestStatus.Fail => "不合格", TestStatus.Running => "测试中", TestStatus.Measured => "已测", _ => "未判定" } : "—"; public object ConvertBack(object value, Type targetType, object? parameter, CultureInfo culture) => Binding.DoNothing; } /// bool → Visibility(参数 "Invert" 取反)。 public sealed class BoolToVisConverter : IValueConverter { public object Convert(object value, Type targetType, object? parameter, CultureInfo culture) { bool b = value is bool x && x; if (parameter is string s && s.Equals("Invert", StringComparison.OrdinalIgnoreCase)) b = !b; return b ? Visibility.Visible : Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object? parameter, CultureInfo culture) => value is Visibility v && v == Visibility.Visible; } /// bool 取反;用于 IsEnabled 这类布尔属性。 public sealed class InverseBoolConverter : IValueConverter { public object Convert(object value, Type targetType, object? parameter, CultureInfo culture) { bool b = value is bool x && x; return !b; } public object ConvertBack(object value, Type targetType, object? parameter, CultureInfo culture) { bool b = value is bool x && x; return !b; } } /// null → Collapsed,非 null → Visible(参数 "Invert" 取反)。 public sealed class NullToVisConverter : IValueConverter { public object Convert(object value, Type targetType, object? parameter, CultureInfo culture) { bool isNull = value == null; if (parameter is string s && s.Equals("Invert", StringComparison.OrdinalIgnoreCase)) isNull = !isNull; return isNull ? Visibility.Collapsed : Visibility.Visible; } public object ConvertBack(object value, Type targetType, object? parameter, CultureInfo culture) => Binding.DoNothing; } /// UiLogLevel → 颜色。 public sealed class LogLevelToBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object? parameter, CultureInfo culture) { var key = value is UiLogLevel l ? l switch { UiLogLevel.Success => "SuccessBrush", UiLogLevel.Warning => "WarningBrush", UiLogLevel.Error => "DangerBrush", _ => "TextPrimaryBrush" } : "TextPrimaryBrush"; return Application.Current.TryFindResource(key) as Brush ?? Brushes.Black; } public object ConvertBack(object value, Type targetType, object? parameter, CultureInfo culture) => Binding.DoNothing; } /// 数值 → "—"(NaN/0 时)或格式化字符串。参数为 .NET 格式串(如 "F2")。 public sealed class NumberOrDashConverter : IValueConverter { public object Convert(object value, Type targetType, object? parameter, CultureInfo culture) { if (value is double d) { if (double.IsNaN(d)) return "—"; return d.ToString(parameter as string ?? "F2", CultureInfo.InvariantCulture); } return "—"; } public object ConvertBack(object value, Type targetType, object? parameter, CultureInfo culture) => Binding.DoNothing; }