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