2026-08-10 14:45:47 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using Avalonia.Data.Converters;
|
|
|
|
|
using Avalonia.Media;
|
|
|
|
|
|
|
|
|
|
namespace Avalonia.PropertyGrid.Controls;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts an Avalonia.Media.Color to a SolidColorBrush
|
|
|
|
|
/// </summary>
|
2026-08-13 09:49:27 +02:00
|
|
|
public sealed class ColorToBrushConverter : IValueConverter
|
2026-08-10 14:45:47 +02:00
|
|
|
{
|
2026-08-13 09:49:27 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a Color to a SolidColorBrush.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">The Color value to convert.</param>
|
|
|
|
|
/// <param name="targetType">The target type.</param>
|
|
|
|
|
/// <param name="parameter">Optional parameter.</param>
|
|
|
|
|
/// <param name="culture">Culture information.</param>
|
|
|
|
|
/// <returns>A SolidColorBrush with the specified color.</returns>
|
2026-08-10 14:45:47 +02:00
|
|
|
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
if (value is Color color)
|
|
|
|
|
{
|
|
|
|
|
return new SolidColorBrush(color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return transparent brush if not a color
|
|
|
|
|
return new SolidColorBrush(Colors.Transparent);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-13 09:49:27 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a SolidColorBrush back to a Color.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">The SolidColorBrush value to convert back.</param>
|
|
|
|
|
/// <param name="targetType">The target type.</param>
|
|
|
|
|
/// <param name="parameter">Optional parameter.</param>
|
|
|
|
|
/// <param name="culture">Culture information.</param>
|
|
|
|
|
/// <returns>The Color from the brush.</returns>
|
2026-08-10 14:45:47 +02:00
|
|
|
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
|
|
|
|
{
|
2026-08-13 09:49:27 +02:00
|
|
|
if (value is SolidColorBrush brush)
|
2026-08-10 14:45:47 +02:00
|
|
|
{
|
|
|
|
|
return brush.Color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Colors.Transparent;
|
|
|
|
|
}
|
|
|
|
|
}
|