简体   繁体   English

Encoding属性的属性编辑器

[英]Property editor for Encoding property

I'm implementing a custom TextBox and would like its new encoding property to be editable in the designer. 我正在实现一个自定义TextBox,并希望它的新编码属性可以在设计器中编辑。

Here is the declaration of the property: 这是财产的声明:

    private Encoding tbEnc;
    public Encoding tbEncoding { get { return tbEnc; } set { tbEnc = value; } }

It shows up in the property grid alright but disabled. 它显示在属性网格中,但是已禁用。 I had hoped it would work out of the box as Encoding is a standard type, like, say Font, for which the standard editor comes up. 我曾希望它可以开箱即用,因为Encoding是一种标准类型,比如Font,标准编辑器出现了。

Do I have to build a UITypeEditor and what would be the simplest implementation? 我是否必须构建一个UITypeEditor以及最简单的实现?

The disabled state on the property means that NET cant find a match for the Type, namely System.Text.Encoding . 属性上的禁用状态意味着NET无法找到Type的匹配项,即System.Text.Encoding Since there are other properties there you probably would not want to show (like all the Isxxxxxxx props), a stock/default editor probably would not do what you want anyway. 由于还有其他属性,你可能不想显示(像所有Isxxxxxxx道具),股票/默认编辑器可能不会做你想要的任何事情。

One way would be to use a TypeConverter but for something like this, just exposing the property as an enum can be an advantage: 一种方法是使用TypeConverter但是对于这样的事情,只是将属性暴露为枚举可能是一个优点:

  • You may not want to support all the options available in the NET Type 您可能不希望支持NET类型中的所有可用选项
  • you can use different, perhaps friendlier names/text if desired 如果需要,您可以使用不同的,可能更友好的名称/文本
  • As an enum NET will use a dropdown so you dont have to write anything 作为一个枚举NET将使用下拉列表,所以你不必写任何东西

class newTB : TextBox
{
    // encoding subset to implement
    public enum NewTBEncoding
    {
        ASCII, UTF8, UTF7
    };

    // prop as enum
    private NewTBEncoding tbEnc;

    public NewTBEncoding tbEncoding { 
        get { return tbEnc; } 
        set {tbEnc = value; }
    }
}

You sometimes have to do a conversion from the enum value to the actual underlying Type, but that is often a one time thing you can do in the property setter or simply when used. 您有时必须从枚举值转换为实际的基础类型,但这通常是您可以在属性设置器中执行的一次性操作,或者只是在使用时。 Result: 结果:

http://i.imgur.com/fVKXbjl.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM