如何强制使用自定义UITypeEditor进行系统类型

Pau*_*rry 4 .net propertygrid

我有一个自定义UITypeEditor用于我的程序使用propertygrid进行颜色选择,但如果我只是暴露system.drawing.color,我似乎无法激活它.我需要在调用我的UITypeEditor之前用CustomType包装Color.

注意属性TheColour它的工作原理.该颜色没有.

当我打开propertyGrid时,我可以看到GetEditStyle通过这两种方法调用,但是当它出现时,EditValue只有在propertygrid中选择TheColour时才会调用它.选择"颜色属性"时,将显示"正常颜色"下拉列表

我错过了什么?

<CategoryAttribute("Order Colour"), _
 Browsable(True), _
 DisplayName("The Colour"), _
 Description("The background colour for orders from this terminal"), _
EditorAttribute(GetType(IKMDependency.ColourSelectorEditor), _ 
GetType(System.Drawing.Design.UITypeEditor))> _
Public Property TheColour() As MyColour
    Get
        Return mMyColor
    End Get
    Set(ByVal value As MyColour)
        If value.Colour <> mMyColor.Colour Then
            mColor = value.Colour
            mMyColor = value
            mIsDirty = True
        End If
    End Set
End Property

<CategoryAttribute("Order Colour"), _
 Browsable(True), _
 DisplayName("Colour"), _
 Description("The background colour for orders from this terminal"), _
EditorAttribute(GetType(IKMDependency.ColourSelectorEditor), _ 
GetType(System.Drawing.Design.UITypeEditor))> _
Public Property Colour() As Color
    Get
        Return mColor
    End Get
    Set(ByVal value As Color)
        If mColor <> value Then
            mColor = value
            mMyColor = New MyColour(mColor)
            mIsDirty = True
        End If
    End Set
End Property
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 6

问题是它注意到相关的TypeConverter支持枚举值.我们需要禁用它; 请注意,我们也可以继承默认实现,以获得颜色预览绘画(C#中的示例,但应该很容易翻译):

class MyColorEditor : ColorEditor {
    public override UITypeEditorEditStyle GetEditStyle(
        ITypeDescriptorContext context) {
         return UITypeEditorEditStyle.Modal;
    }
    public override object  EditValue(
       ITypeDescriptorContext context, IServiceProvider provider, object value) {
        MessageBox.Show(
              "We could show an editor here, but you meant Green, right?");
       return Color.Green;
    }
}
class MyColorConverter : ColorConverter { // reference: System.Drawing.Design.dll
    public override bool GetStandardValuesSupported(
            ITypeDescriptorContext context) {
        return false;
    }
}
class TestObject
{
    [Category("Order Colour"), Browsable(true), DisplayName("Colour")]
    [Description("The background colour for orders from this terminal")]
    [Editor(typeof(MyColorEditor), typeof(UITypeEditor))]
    [TypeConverter(typeof(MyColorConverter))]
    public Color Colour {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

如果你想将它应用于所有 Color属性,还有一种方法可以做到这一点,你不需要装饰每个属性; 在应用程序的初始化代码中的某个地方,执行:

TypeDescriptor.AddAttributes(typeof(Color),
    new EditorAttribute(typeof(MyColorEditor), typeof(UITypeEditor)),
    new TypeConverterAttribute(typeof(MyColorConverter)));
Run Code Online (Sandbox Code Playgroud)