C# PropertyGrid:仅为特定属性移除自定义数据类型的属性

C# PropertyGrid:仅为特定属性移除自定义数据类型的属性,c#,.net,vb.net,winforms,uitypeeditor,C#,.net,Vb.net,Winforms,Uitypeeditor,第一:问题的措辞可能不准确,很抱歉。实际问题在所有代码片段下面。 第二:代码是用C#编写的,但我通常用VB.NET编写代码 我有一个类LabelData,它包含用户绘制标签的视觉外观数据。简短示例: public class LabelData { public Color BackColor1 { get; set; } public Color BackColor2 { get; set; } public Color TextColor { get; set; }

第一:问题的措辞可能不准确,很抱歉。实际问题在所有代码片段下面。
第二:代码是用C#编写的,但我通常用VB.NET编写代码

我有一个类
LabelData
,它包含用户绘制标签的视觉外观数据。简短示例:

public class LabelData
{
    public Color BackColor1 { get; set; }
    public Color BackColor2 { get; set; }
    public Color TextColor { get; set; }
    public int TextAngle { get; set; }
    public string Text { get; set; }
}
My
UserControl
使用
LabelData
绘制大量文本(称为小标签)。它看起来像这样(简化):

现在的问题是:
PropertyGrid
LabelTemplate
属性的
Text
-属性(而不是
Title
属性)在
OnPaint
中发生更改,如何删除该属性


PS:我试图创建一个自定义设计器并覆盖
预过滤器属性
以删除
文本
属性,但我无法将
设计器属性
添加到
标签模板
属性。

因为您可能需要一个属性来显示从
标签数据
类对象分配的属性,您可以基于
ExpandableObjectConverter
创建自定义TypeConverter,并从基类对象中删除属性,如果您希望在特定情况下以不同的方式呈现此对象

这里,作为示例,
标题
模板
属性都显示为PropertyGrid中的可扩展对象(因此您可以使用其特定类型编辑器编辑每个属性值),
模板
属性具有稍微不同的类型转换器,其中重写该方法以从基础类对象中删除
Text
属性,因此它不会显示在PropertyGrid中:

“模板可展开对象”属性未将“文本”属性显示为其“标题”属性

C#版本

public class UserControl1
{
    public UserControl1() {
        Template = new LabelData() { ... };
        Title = new LabelData() { ... };
    }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public LabelData Title { get; set; }

    [TypeConverter(typeof(CustomExpandableConverter))]
    public LabelData Template { get; set; }

    // [...]
}

public class CustomExpandableConverter : ExpandableObjectConverter
{
    public CustomExpandableConverter() { }

    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
    {
        var props = base.GetProperties(context, value, attributes)
                        .OfType<PropertyDescriptor>().Where(pd => pd.Name != "Text").ToArray();
        return new PropertyDescriptorCollection(props);
    }
}
公共类UserControl1
{
公共用户控制1(){
Template=新LabelData(){…};
Title=新LabelData(){…};
}
[TypeConverter(typeof(ExpandableObjectConverter))]
公共LabelData标题{get;set;}
[TypeConverter(typeof(CustomExpandableConverter))]
公共LabelData模板{get;set;}
// [...]
}
公共类CustomExpandableConverter:ExpandableObjectConverter
{
公共CustomExpandableConverter(){}
公共重写属性描述或集合GetProperties(ITypeDescriptorContext上下文、对象值、属性[]属性)
{
var props=base.GetProperties(上下文、值、属性)
.OfType().Where(pd=>pd.Name!=“Text”).ToArray();
返回新的PropertyDescriptorCollection(道具);
}
}
VB.Net版本

public class UserControl1
{
    public UserControl1() {
        Template = new LabelData() { ... };
        Title = new LabelData() { ... };
    }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public LabelData Title { get; set; }

    [TypeConverter(typeof(CustomExpandableConverter))]
    public LabelData Template { get; set; }

    // [...]
}

public class CustomExpandableConverter : ExpandableObjectConverter
{
    public CustomExpandableConverter() { }

    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
    {
        var props = base.GetProperties(context, value, attributes)
                        .OfType<PropertyDescriptor>().Where(pd => pd.Name != "Text").ToArray();
        return new PropertyDescriptorCollection(props);
    }
}
公共类UserControl1
公共分新()
模板=新LabelData()
Title=新LabelData()
端接头
作为LabelData的公共财产所有权
作为LabelData的公共属性模板
'[...]
末级
公共类CustomExpandableConverter
继承ExpandableObjectConverter
公共分新()
端接头
公共重写函数GetProperties(上下文作为ITypeDescriptorContext,值作为对象,属性()作为属性)作为PropertyDescriptorCollection
Dim props=MyBase.GetProperties(上下文、值、属性)。
类型(PropertyDescriptor的)()。
其中(Function(pd)Not pd.Name.Equals(“Text”)).ToArray()
返回新属性DescriptorCollection(道具)
端函数
末级

我不知道你可以在
类型转换器中过滤它们。这太棒了。我过滤了更多的属性,因为在
LabelData
类中实际上有更多的属性。非常感谢你。
public class UserControl1
{
    public UserControl1() {
        Template = new LabelData() { ... };
        Title = new LabelData() { ... };
    }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public LabelData Title { get; set; }

    [TypeConverter(typeof(CustomExpandableConverter))]
    public LabelData Template { get; set; }

    // [...]
}

public class CustomExpandableConverter : ExpandableObjectConverter
{
    public CustomExpandableConverter() { }

    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
    {
        var props = base.GetProperties(context, value, attributes)
                        .OfType<PropertyDescriptor>().Where(pd => pd.Name != "Text").ToArray();
        return new PropertyDescriptorCollection(props);
    }
}