using System; using System.Globalization; using System.Windows.Data; using System.Windows.Media; namespace XP.Hardware.PLC.Sentry.Converters { /// /// 连接状态文本到指示灯颜色转换器 | Connection status text to indicator color converter /// 已连接→绿色,重连中→黄色,其他→灰色 | Connected→Green, Reconnecting→Yellow, Others→Gray /// public class ConnectionStatusToColorConverter : IMultiValueConverter { /// /// 已连接颜色(绿色)| Connected color (green) /// private static readonly SolidColorBrush ConnectedBrush = new(Color.FromRgb(0x4C, 0xAF, 0x50)); /// /// 重连中颜色(黄色)| Reconnecting color (yellow) /// private static readonly SolidColorBrush ReconnectingBrush = new(Color.FromRgb(0xFF, 0xC1, 0x07)); /// /// 未连接颜色(灰色)| Disconnected color (gray) /// private static readonly SolidColorBrush DisconnectedBrush = new(Color.FromRgb(0xBD, 0xBD, 0xBD)); public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { if (values.Length >= 2 && values[0] is bool isConnected && values[1] is string statusText) { if (isConnected) return ConnectedBrush; // 检查状态文本是否包含"重连"关键字 | Check if status text contains reconnecting keyword if (statusText != null && (statusText.Contains("重连") || statusText.Contains("Reconnect", StringComparison.OrdinalIgnoreCase))) return ReconnectingBrush; } return DisconnectedBrush; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }