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.
121 lines
3.7 KiB
121 lines
3.7 KiB
using Avalonia.Data.Converters;
|
|
using Avalonia.Media;
|
|
using HeroIconsAvalonia.Enums;
|
|
using System;
|
|
using System.Globalization;
|
|
|
|
namespace MyAvaloniaApp.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 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 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();
|
|
}
|
|
}
|
|
|