class ThemeColors { public static Themes Theme { get; set; } = Themes.Light;
public static Color Background => (Theme == Themes.Light ? Color.White : Color.Black); public static Color Foreground => (Theme == Themes.Light ? Color.Black : Color.White); }
class ThemeColorConverter : TypeConverter { public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) => new StandardValuesCollection(typeof(ThemeColors).GetProperties().Where(p => p.PropertyType == typeof(Color)).Select(p => p.Name).ToList());
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) return typeof(ThemeColors).GetProperty(value.ToString())?.GetValue(null); return base.ConvertFrom(context, culture, value); }
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(InstanceDescriptor)) return true; return base.CanConvertTo(context, destinationType); }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(InstanceDescriptor)) return new InstanceDescriptor(typeof(ThemeColors).GetProperty(value.ToString()), null); return base.ConvertTo(context, culture, value, destinationType); } }
class TestControl : Control { [Editor(typeof(UITypeEditor), typeof(UITypeEditor))] [TypeConverter(typeof(ThemeColorConverter))] public override Color BackColor { get => base.BackColor ; set => base.BackColor = value; }
[Editor(typeof(UITypeEditor), typeof(UITypeEditor))] [TypeConverter(typeof(ThemeColorConverter))] public override Color ForeColor { get => base.ForeColor ; set => base.ForeColor = value; } }