93 lines
2.9 KiB
C#
93 lines
2.9 KiB
C#
using System;
|
|||
|
|
using System.Collections.Generic;
|
||
|
|
using Avalonia.PropertyGrid.Models;
|
||
|
|
|
||
|
|
namespace Avalonia.PropertyGrid.Editors;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Registry for custom property editors
|
||
|
|
/// </summary>
|
||
|
|
public static class EditorRegistry
|
||
|
|
{
|
||
|
|
private static readonly Dictionary<Type, Type> _typeEditors = new();
|
||
|
|
private static readonly Dictionary<string, Type> _propertyEditors = new();
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Register a custom editor for a specific type
|
||
|
|
/// </summary>
|
||
|
|
public static void RegisterEditor(Type propertyType, Type editorType)
|
||
|
|
{
|
||
|
|
if (!typeof(PropertyEditor).IsAssignableFrom(editorType))
|
||
|
|
{
|
||
|
|
throw new ArgumentException($"Editor type must inherit from PropertyEditor", nameof(editorType));
|
||
|
|
}
|
||
|
|
|
||
|
|
_typeEditors[propertyType] = editorType;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Register a custom editor for a specific property by name
|
||
|
|
/// </summary>
|
||
|
|
public static void RegisterEditor(string propertyName, Type editorType)
|
||
|
|
{
|
||
|
|
if (!typeof(PropertyEditor).IsAssignableFrom(editorType))
|
||
|
|
{
|
||
|
|
throw new ArgumentException($"Editor type must inherit from PropertyEditor", nameof(editorType));
|
||
|
|
}
|
||
|
|
|
||
|
|
_propertyEditors[propertyName] = editorType;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Get the editor type for a property item
|
||
|
|
/// </summary>
|
||
|
|
public static Type? GetEditorType(PropertyItem item)
|
||
|
|
{
|
||
|
|
// Check for explicit editor type on the item
|
||
|
|
if (item.EditorType != null)
|
||
|
|
{
|
||
|
|
return item.EditorType;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Check for property-specific editor
|
||
|
|
if (_propertyEditors.TryGetValue(item.Name, out var propertyEditor))
|
||
|
|
{
|
||
|
|
return propertyEditor;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Check for type-specific editor
|
||
|
|
if (_typeEditors.TryGetValue(item.PropertyType, out var typeEditor))
|
||
|
|
{
|
||
|
|
return typeEditor;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Return default editor based on type
|
||
|
|
return GetDefaultEditorType(item.PropertyType);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static Type? GetDefaultEditorType(Type propertyType)
|
||
|
|
{
|
||
|
|
if (propertyType == typeof(string))
|
||
|
|
return typeof(StringEditor);
|
||
|
|
|
||
|
|
if (propertyType == typeof(bool))
|
||
|
|
return typeof(BooleanEditor);
|
||
|
|
|
||
|
|
if (propertyType == typeof(int) || propertyType == typeof(long) ||
|
||
|
|
propertyType == typeof(short) || propertyType == typeof(byte) ||
|
||
|
|
propertyType == typeof(uint) || propertyType == typeof(ulong) ||
|
||
|
|
propertyType == typeof(ushort) || propertyType == typeof(sbyte) ||
|
||
|
|
propertyType == typeof(float) || propertyType == typeof(double) ||
|
||
|
|
propertyType == typeof(decimal))
|
||
|
|
return typeof(NumericEditor);
|
||
|
|
|
||
|
|
if (propertyType.IsEnum)
|
||
|
|
return typeof(EnumEditor);
|
||
|
|
|
||
|
|
if (propertyType == typeof(DateTime))
|
||
|
|
return typeof(DateTimeEditor);
|
||
|
|
|
||
|
|
return null; // Will use default TextBox
|
||
|
|
}
|
||
|
|
}
|