Create UInt32ToColorConverter.cs

This commit is contained in:
Wiesław Šoltés 2022-09-01 09:37:45 +02:00
Родитель beee3ba8a9
Коммит b523f391fe
1 изменённых файлов: 44 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,44 @@
using System;
using System.Globalization;
using Avalonia;
using Avalonia.Data.Converters;
using Avalonia.Media;
namespace ColorPickerDemo;
public class UInt32ToColorConverter : IValueConverter
{
public static UInt32ToColorConverter Instance = new();
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is uint c && (targetType == typeof(Color?) || targetType == typeof(Color)))
{
try
{
return Color.FromUInt32(c);
}
catch (Exception)
{
return AvaloniaProperty.UnsetValue;
}
}
return AvaloniaProperty.UnsetValue;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is Color c2 && targetType == typeof(uint))
{
try
{
return c2.ToUint32();
}
catch (Exception)
{
return AvaloniaProperty.UnsetValue;
}
}
return AvaloniaProperty.UnsetValue;
}
}