public class MyLabel : Label { [Editor(typeof(MyLabelTypeEditor), typeof(UITypeEditor))] public override Color ForeColor { get => base.ForeColor; set => base.ForeColor = value; } }
public class MyLabelTypeEditor : UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) => UITypeEditorEditStyle.DropDown;
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { IWindowsFormsEditorService editorService = null; if (provider != null) editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService; if (editorService == null) return value;
var listBox = new ListBox(); listBox.Click += (sender, e) => editorService.CloseDropDown(); foreach (var propertiy in typeof(MyLabelColors).GetProperties()) { listBox.Items.Add(propertiy.Name); } editorService.DropDownControl(listBox);
public class MyLabelColors { public static Color MyBlack { get => Color.FromArgb(1, 1, 1); } public static Color MyWhite { get => Color.FromArgb(254, 254, 254); } }
public class MyLabel : Label { public new MyLabelColor ForeColor { get => _ForeColor; set { _ForeColor = value; base.ForeColor = _ForeColor.Color; } } private MyLabelColor _ForeColor; }
public class MyLabelTypeConverter : TypeConverter { public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; }
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return new StandardValuesCollection(typeof(MyLabelColor).GetProperties().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) { var myLabelColor = typeof(MyLabelColor).GetProperty(value.ToString())?.GetValue(null); if (myLabelColor != null) { return myLabelColor; } else { return new MyLabelColor("UnknownColor", Color.Transparent); } } 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(MyLabelColor).GetProperty(value.ToString()), null); } return base.ConvertTo(context, culture, value, destinationType); } }
[TypeConverter(typeof(MyLabelTypeConverter))] public class MyLabelColor { public static MyLabelColor MyBlack => new MyLabelColor(nameof(MyBlack), Color.FromArgb(1, 1, 1)); public static MyLabelColor MyWhite => new MyLabelColor(nameof(MyWhite), Color.FromArgb(254, 254, 254));
public string Name; public Color Color;
public MyLabelColor(string name, Color color) { this.Name = name; this.Color = color; }
public override string ToString() { return this.Name; } }