SSPC-Tester/SSPCTester.UI/Converters/Converters.cs
2026-07-03 14:16:15 +08:00

137 lines
5.3 KiB
C#
Raw 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.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using SSPCTester.Logic.Models;
using SSPCTester.UI.Services;
namespace SSPCTester.UI.Converters;
/// <summary>TestStatus → 实色徽标背景。</summary>
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;
}
/// <summary>TestStatus → 浅色徽标背景。</summary>
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;
}
/// <summary>TestStatus → 中文文本。</summary>
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;
}
/// <summary>bool → Visibility参数 "Invert" 取反)。</summary>
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;
}
/// <summary>bool 取反;用于 IsEnabled 这类布尔属性。</summary>
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;
}
}
/// <summary>null → Collapsed非 null → Visible参数 "Invert" 取反)。</summary>
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;
}
/// <summary>UiLogLevel → 颜色。</summary>
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;
}
/// <summary>数值 → "—"NaN/0 时)或格式化字符串。参数为 .NET 格式串(如 "F2")。</summary>
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;
}