You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

269 lines
8.3 KiB

using Avalonia.Data.Converters;
using Avalonia.Media;
using HeroIconsAvalonia.Enums;
using System;
using System.Globalization;
namespace AuroraDesk.Presentation.Converters;
/// <summary>
/// 字符串转换器类
/// </summary>
public static class StringConverters
{
/// <summary>
/// 角色到颜色的转换器
/// </summary>
public static readonly IValueConverter RoleToColorConverter = new RoleToColorConverter();
/// <summary>
/// 状态到颜色的转换器
/// </summary>
public static readonly IValueConverter StatusToColorConverter = new StatusToColorConverter();
/// <summary>
/// 空值到布尔值的转换器
/// </summary>
public static readonly IValueConverter NullToBoolConverter = new NullToBoolConverter();
/// <summary>
/// 布尔值到图标类型的转换器
/// </summary>
public static readonly IValueConverter BoolToIconConverter = new BoolToIconConverter();
/// <summary>
/// 布尔值到颜色的转换器(用于连接状态)
/// </summary>
public static readonly IValueConverter BoolToBrushConverter = new BoolToBrushConverter();
/// <summary>
/// 字符串到可见性的转换器(非空则可见)
/// </summary>
public static readonly IValueConverter StringToVisibilityConverter = new StringToVisibilityConverter();
/// <summary>
/// 整数到可见性的转换器(0 则不可见)
/// </summary>
public static readonly IValueConverter IntToVisibilityConverter = new IntToVisibilityConverter();
/// <summary>
/// 字符串非空检查转换器(非空则返回 true)
/// </summary>
public static readonly IValueConverter IsNotNullOrEmptyConverter = new IsNotNullOrEmptyConverter();
/// <summary>
/// 布尔值到可见性转换器
/// </summary>
public static readonly IValueConverter BoolToVisibilityConverter = new BoolToVisibilityConverter();
}
/// <summary>
/// 角色到颜色转换器
/// </summary>
public class RoleToColorConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is string role)
{
return role switch
{
"管理员" => new SolidColorBrush(Color.FromRgb(220, 53, 69)), // 红色
"编辑" => new SolidColorBrush(Color.FromRgb(255, 193, 7)), // 黄色
"用户" => new SolidColorBrush(Color.FromRgb(40, 167, 69)), // 绿色
_ => new SolidColorBrush(Color.FromRgb(108, 117, 125)) // 灰色
};
}
return new SolidColorBrush(Color.FromRgb(108, 117, 125));
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
/// <summary>
/// 状态到颜色转换器
/// </summary>
public class StatusToColorConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is string status)
{
return status switch
{
"在线" => new SolidColorBrush(Color.FromRgb(40, 167, 69)), // 绿色
"离线" => new SolidColorBrush(Color.FromRgb(108, 117, 125)), // 灰色
"忙碌" => new SolidColorBrush(Color.FromRgb(220, 53, 69)), // 红色
_ => new SolidColorBrush(Color.FromRgb(108, 117, 125)) // 灰色
};
}
return new SolidColorBrush(Color.FromRgb(108, 117, 125));
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
/// <summary>
/// 空值到布尔值转换器
/// </summary>
public class NullToBoolConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return value != null;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
/// <summary>
/// 空值到布尔值转换器(取反)
/// </summary>
public class NullToInverseBoolConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return value == null;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
/// <summary>
/// 布尔值到图标类型转换器
/// </summary>
public class BoolToIconConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is bool isExpanded)
{
return isExpanded ? IconType.ChevronDown : IconType.ChevronRight;
}
return IconType.ChevronRight;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
/// <summary>
/// 布尔值到画刷转换器(用于连接状态颜色)
/// </summary>
public class BoolToBrushConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is bool isConnected)
{
return isConnected
? new SolidColorBrush(Color.FromRgb(40, 167, 69)) // 绿色 - 已连接
: new SolidColorBrush(Color.FromRgb(108, 117, 125)); // 灰色 - 未连接
}
return new SolidColorBrush(Color.FromRgb(108, 117, 125));
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
/// <summary>
/// 字符串到可见性转换器(非空则可见)
/// </summary>
public class StringToVisibilityConverter : IValueConverter
{
public static StringToVisibilityConverter Instance { get; } = new();
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return !string.IsNullOrEmpty(value?.ToString());
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
/// <summary>
/// 整数到可见性转换器(0 则不可见,用于空列表提示)
/// </summary>
public class IntToVisibilityConverter : IValueConverter
{
public static IntToVisibilityConverter Instance { get; } = new();
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is int count)
{
return count == 0;
}
return false;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
/// <summary>
/// 字符串非空检查转换器(非空则返回 true)
/// </summary>
public class IsNotNullOrEmptyConverter : IValueConverter
{
public static IsNotNullOrEmptyConverter Instance { get; } = new();
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return !string.IsNullOrEmpty(value?.ToString());
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
/// <summary>
/// 布尔值到可见性转换器
/// </summary>
public class BoolToVisibilityConverter : IValueConverter
{
public static BoolToVisibilityConverter Instance { get; } = new();
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is bool boolValue)
{
// 如果 parameter 是 "Invert",则反转布尔值
if (parameter?.ToString() == "Invert")
{
return !boolValue;
}
return boolValue;
}
return false;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}