如何强制使用自定义UITypeEditor的的系统类型 [英] How to Force use of Custom UITypeEditor for System Types

查看:1501
本文介绍了如何强制使用自定义UITypeEditor的的系统类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义UITypeEditor的是在对颜色的选择与使用使用的PropertyGrid我的计划,但我似乎无法得到,如果我只是暴露的System.Drawing.Color它来激活。我需要包装的颜色与CustomType之前,它会调用我的UITypeEditor的。

请注意 TheColour 它的工作性质。在颜色的没有。

当我打开PropertyGrid中,我可以看到 GetEditStyle 通过这两种方法被调用,但是当涉及到​​的EditValue 这是当你在PropertyGrid中选择TheColour只调用。在正常颜色下拉列表显示在选择颜色属性

我在想什么?

 <的CategoryAttribute(订单知多少),_
 可浏览(真),_
 显示名称(以下简称颜色),_
 说明(以下简称背景颜色从该终端订单),_
EditorAttribute(的GetType(IKMDependency.ColourSelectorEditor),_
的GetType(System.Drawing.Design.UITypeEditor))> _
公共财产TheColour()作为MyColour
    得到
        返回mMyColor
    最终获取
    设置(BYVAL值作为MyColour)
        如果value.Colour<> mMyColor.Colour然后
            mColor = value.Colour
            mMyColor =价值
            mIsDirty = TRUE
        结束如果
    结束设定
高端物业

<的CategoryAttribute(订单知多少),_
 可浏览(真),_
 显示名称(颜色),_
 说明(以下简称背景颜色从该终端订单),_
EditorAttribute(的GetType(IKMDependency.ColourSelectorEditor),_
的GetType(System.Drawing.Design.UITypeEditor))> _
公共属性颜色()作为颜色
    得到
        返回mColor
    最终获取
    设置(BYVAL值作为彩色)
        如果mColor<>值。然后
            mColor =价值
            mMyColor =新MyColour(mColor)
            mIsDirty = TRUE
        结束如果
    结束设定
高端物业
 

解决方案

现在的问题是,它已经注意到了相关的类型转换器支持枚举值。我们需要禁用;注意,我们还可以的继承的从默认的实现得到的东西喜欢的颜色preVIEW绘画(例子在C#中,但应该很容易翻译):

 类MyColorEditor:ColorEditor {
    公众覆盖UITypeEditorEditStyle GetEditStyle(
        ITypeDescriptorContext上下文){
         返回UITypeEditorEditStyle.Modal;
    }
    公众覆盖对象的EditValue(
       ITypeDescriptorContext背景下,IServiceProvider的提供者,对象的值){
        的MessageBox.show(
              我们可以显示一个编辑在这里,但你的意思是绿色,是吗?);
       返回Color.Green;
    }
}
类MyColorConverter:ColorConverter {//参考:System.Drawing.Design.dll
    公众覆盖布尔GetStandardValues​​Supported(
            ITypeDescriptorContext上下文){
        返回false;
    }
}
一流的TestObject
{
    [类别(订单知多少),可浏览(真),显示名称(颜色)
    [说明(以下简称背景颜色从该终端订单)
    
    [类型转换器(typeof运算(MyColorConverter))]
    众彩颜色{获取;集;}
}
 

如果您希望此申请的所有颜色的属性,也有办法做到这一点,这样你就不需要装点每个属性;在某处你的应用程序的初始化code,执行:

  TypeDescriptor.AddAttributes(typeof运算(彩色),
    新EditorAttribute(typeof运算(MyColorEditor)的typeof(UITypeEditor的)),
    新TypeConverterAttribute(typeof运算(MyColorConverter)));
 

I have a custom UITypeEditor that is in use for Colour Selection with my program using the propertygrid, but I can't seem to get it to activate if I just expose system.drawing.color. I need to wrap the Color with a CustomType before it will invoke my UITypeEditor.

Note the Property TheColour it works. The Colour doesn't.

When I open the propertyGrid, I can see GetEditStyle is called via both methods, but when it comes to EditValue it is only called when you select TheColour in the propertygrid. The Normal Colour dropdown is shown when you select Colour Property

What am I missing?

<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

解决方案

The problem is that it is noticing that the associated TypeConverter supports enumerated values. We need to disable that; note we can also inherit from the default implementations to get things like the color preview painting (examples in C#, but should be easy to translate):

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;}
}

If you want this to apply to all Color properties, there is also a way to do it such that you don't need to decorate every property; somewhere during your app's initialization code, execute:

TypeDescriptor.AddAttributes(typeof(Color),
    new EditorAttribute(typeof(MyColorEditor), typeof(UITypeEditor)),
    new TypeConverterAttribute(typeof(MyColorConverter)));

这篇关于如何强制使用自定义UITypeEditor的的系统类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆