initial commit
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
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>
|
||||
public class ColorToBrushConverter : IValueConverter
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is SolidColorBrush brush && brush.Color != null)
|
||||
{
|
||||
return brush.Color;
|
||||
}
|
||||
|
||||
return Colors.Transparent;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Avalonia.Data.Converters;
|
||||
|
||||
namespace Avalonia.PropertyGrid.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Converts boolean value to its negation for two-way binding
|
||||
/// </summary>
|
||||
public class NegateBoolConverter : IValueConverter
|
||||
{
|
||||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is bool boolValue)
|
||||
{
|
||||
return !boolValue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is bool boolValue)
|
||||
{
|
||||
return !boolValue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Avalonia.Data.Converters;
|
||||
|
||||
namespace Avalonia.PropertyGrid.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Converts PropertyType to appropriate numeric format string
|
||||
/// </summary>
|
||||
public class NumericFormatConverter : IValueConverter
|
||||
{
|
||||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is Type type)
|
||||
{
|
||||
// Floating point types use decimal places
|
||||
if (type == typeof(float) || type == typeof(double) || type == typeof(decimal))
|
||||
{
|
||||
return "N2"; // 2 decimal places
|
||||
}
|
||||
|
||||
// Integer types use no decimal places
|
||||
return "N0";
|
||||
}
|
||||
|
||||
return "N0";
|
||||
}
|
||||
|
||||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Avalonia.Data.Converters;
|
||||
|
||||
namespace Avalonia.PropertyGrid.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Converts indent level to left margin
|
||||
/// </summary>
|
||||
public class IndentToMarginConverter : IValueConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts indent level to left margin.
|
||||
/// </summary>
|
||||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is int indentLevel)
|
||||
{
|
||||
return new Thickness(indentLevel * 16, 0, 0, 0);
|
||||
}
|
||||
return new Thickness(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not implemented.
|
||||
/// </summary>
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts enum type to list of enum values
|
||||
/// </summary>
|
||||
public class EnumValuesConverter : IValueConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts an enum type to an array of its values.
|
||||
/// </summary>
|
||||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is Type enumType && enumType.IsEnum)
|
||||
{
|
||||
return Enum.GetValues(enumType);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not implemented.
|
||||
/// </summary>
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a type is an enum
|
||||
/// </summary>
|
||||
public class IsEnumConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is Type type)
|
||||
{
|
||||
return type.IsEnum;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a type is a simple type (string, number, bool, etc.)
|
||||
/// </summary>
|
||||
public class IsSimpleTypeConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is Type type)
|
||||
{
|
||||
return type == typeof(string) ||
|
||||
type == typeof(int) || type == typeof(long) || type == typeof(short) ||
|
||||
type == typeof(uint) || type == typeof(ulong) || type == typeof(ushort) ||
|
||||
type == typeof(byte) || type == typeof(sbyte) ||
|
||||
type == typeof(float) || type == typeof(double) || type == typeof(decimal) ||
|
||||
type == typeof(DateTime) || type == typeof(TimeSpan) || type == typeof(Guid) ||
|
||||
type == typeof(DateTimeOffset) || !type.IsEnum;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a type matches the specified type name
|
||||
/// </summary>
|
||||
public class TypeToBoolConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is Type type && parameter is string typeName)
|
||||
{
|
||||
return type.FullName == typeName || type.Name == typeName;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts Color to/from hex string representation
|
||||
/// </summary>
|
||||
public class ColorToStringConverter : IValueConverter
|
||||
{
|
||||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is Media.Color color)
|
||||
{
|
||||
return $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}";
|
||||
}
|
||||
return value?.ToString();
|
||||
}
|
||||
|
||||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is string hexString && !string.IsNullOrWhiteSpace(hexString))
|
||||
{
|
||||
try
|
||||
{
|
||||
hexString = hexString.Trim();
|
||||
if (!hexString.StartsWith("#"))
|
||||
hexString = "#" + hexString;
|
||||
|
||||
return Media.Color.Parse(hexString);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// If parsing fails, return the original color or default
|
||||
return Media.Colors.Black;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a type is Color
|
||||
/// </summary>
|
||||
public class IsColorTypeConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is Type type)
|
||||
{
|
||||
bool isColor = type == typeof(Media.Color);
|
||||
|
||||
// Support inverting the result
|
||||
if (parameter is string param && param.Equals("Invert", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return !isColor;
|
||||
}
|
||||
|
||||
return isColor;
|
||||
}
|
||||
|
||||
// Default to showing TextBox (inverted result)
|
||||
if (parameter is string param2 && param2.Equals("Invert", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a type is numeric
|
||||
/// </summary>
|
||||
public class IsNumericTypeConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is Type type)
|
||||
{
|
||||
bool isNumeric = type == typeof(int) || type == typeof(long) || type == typeof(short) ||
|
||||
type == typeof(byte) || type == typeof(uint) || type == typeof(ulong) ||
|
||||
type == typeof(ushort) || type == typeof(sbyte) ||
|
||||
type == typeof(float) || type == typeof(double) || type == typeof(decimal);
|
||||
|
||||
// Support inverting the result
|
||||
if (parameter is string param && param.Equals("Invert", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return !isNumeric;
|
||||
}
|
||||
|
||||
return isNumeric;
|
||||
}
|
||||
|
||||
// Default to showing TextBox (inverted result)
|
||||
if (parameter is string param2 && param2.Equals("Invert", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a type is neither Color nor Numeric nor Boolean nor Enum (for fallback TextBox)
|
||||
/// </summary>
|
||||
public class IsOtherTypeConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is Type type)
|
||||
{
|
||||
bool isColor = type == typeof(Media.Color);
|
||||
bool isNumeric = type == typeof(int) || type == typeof(long) || type == typeof(short) ||
|
||||
type == typeof(byte) || type == typeof(uint) || type == typeof(ulong) ||
|
||||
type == typeof(ushort) || type == typeof(sbyte) ||
|
||||
type == typeof(float) || type == typeof(double) || type == typeof(decimal);
|
||||
bool isBool = type == typeof(bool);
|
||||
bool isEnum = type.IsEnum;
|
||||
|
||||
// Return true if it's neither color nor numeric nor boolean nor enum
|
||||
return !isColor && !isNumeric && !isBool && !isEnum;
|
||||
}
|
||||
|
||||
return true; // Default to showing TextBox
|
||||
}
|
||||
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a type is boolean
|
||||
/// </summary>
|
||||
public class IsBooleanTypeConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is Type type)
|
||||
{
|
||||
bool isBool = type == typeof(bool);
|
||||
|
||||
// Support inverting the result
|
||||
if (parameter is string param && param.Equals("Invert", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return !isBool;
|
||||
}
|
||||
|
||||
return isBool;
|
||||
}
|
||||
|
||||
// Default to showing TextBox (inverted result)
|
||||
if (parameter is string param2 && param2.Equals("Invert", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user