代码之家  ›  专栏  ›  技术社区  ›  Paul Farry

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

  •  4
  • Paul Farry  · 技术社区  · 15 年前

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

    注意属性 颜色 颜色

    当我打开酒店的时候,我可以看到 GetEditStyle 通过两种方法调用,但当涉及到 EditValue

    我错过了什么?

    <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
    
    2 回复  |  直到 15 年前
        1
  •  6
  •   Marc Gravell    15 年前

    问题是它注意到 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;}
    }
    

    全部的 Color 属性,也有一种方法可以这样做,您不需要装饰每个属性;在应用程序的初始化代码中的某个地方,执行:

    TypeDescriptor.AddAttributes(typeof(Color),
        new EditorAttribute(typeof(MyColorEditor), typeof(UITypeEditor)),
        new TypeConverterAttribute(typeof(MyColorConverter)));
    
        2
  •  0
  •   Paul Farry    15 年前

    我需要实现一个TypeConverter来强制支持的GetStandardValuesSupported返回false。

    然后我就可以去掉颜色属性,直接使用。

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

    这有点难看,因为框中的颜色表示为所选的颜色,但文本打印在PropertyGridCell的其余部分中,因此我还向转换器添加了一些重写以仅返回空字符串。(我宁愿把整个牢房都涂成这种颜色,而不仅仅是小盒子)。

    转换器类。

    Public Class ColourTypeConverter
        Inherits TypeConverter
    
        Public Overrides Function GetStandardValuesSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
            Return False
        End Function
    
        Public Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean
            If sourceType Is GetType(String) Then Return False
            Return MyBase.CanConvertFrom(context, sourceType)
        End Function
    
        Public Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
            If destinationType Is GetType(String) Then Return String.Empty
            Return MyBase.ConvertTo(context, culture, value, destinationType)
        End Function
    
        Public Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
            Return MyBase.ConvertFrom(context, culture, value)
        End Function
    
        Public Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
            If destinationType Is GetType(String) Then Return False
            Return MyBase.CanConvertTo(context, destinationType)
        End Function
    End Class
    
    推荐文章