C#的PropertyGrid控件支持给属性项自定义Converter和Editor,当指定从TypeConverter派生的Converter 时可以只能从下拉列表中选择,即属性值不可手工编辑,但不支持UserControl;而指定从UITypeEditor派生的Editor可以支持UserControl但又没办法限制用户直接编辑属性值。
    有时我们需要实现既能从UserControl中设定值又不想让用户直接编辑,就象一些Collection一样,只显示"(Collection)"串不可编辑又可以通过下拉或弹出窗体让用户选择。经过多次实现,发现属性项同时指定自定义Converter和Editor可以解决这个问题,后面附一些实现代码。
    定义属性项:
InBlock.gif                 private ItemContent _itemContent = new ItemContent();
InBlock.gif                [EditorAttribute( typeof(ContentEditor), typeof(UITypeEditor)),
InBlock.gif                TypeConverterAttribute( typeof(ConentConverter)),
InBlock.gif                DescriptionAttribute( "Select item content")]
InBlock.gif                 public ItemContent Content
InBlock.gif                {
InBlock.gif                        get { return _itemContent; }
InBlock.gif                        set { _itemContent = value; }
InBlock.gif                }
    定义属性内容类:
InBlock.gif         public class ItemContent
InBlock.gif        {