自定义属性与子属性在Visual Studio的属性面板属性、自定义、与子、面板

2023-09-05 03:20:57 作者:刺客的叔叔.

我一直没能找到这个文件主要是由于这样的事实,我不知道是什么的完全的搜索。但我已经看到它是如何在之前,所以我能获得一些属性显示在属性面板在Visual Studio中的一些方法做的,但是我现在需要添加一个属性与子属性在属性面板。现在我回到了起点 - 不知道该怎么寻找,不能够实现它,因为我无法找到相关信息。

I have not been able to find documentation on this mostly due to the fact that I don't know what exactly to search for. But I have seen how it's done in some ways before so I was able to get some properties to show up in the Properties Pane in Visual Studio, however I now need to add a Property with sub-properties in the Properties Pane. I am now back at square one - not knowing what to search for and not being able to implement it because I cannot find relevant information.

在属性窗格中的大多数控件,你会看到这样的:

In the Properties pane for most Controls, you'll see this:

外观

背景色 前景色 字体 字体家庭 字体大小,等等等等。 BackColor ForeColor Font Font Family Font Size, etc etc.

我已经可以做到这一点:

I can already do this:

外观

背景色 前景色 万岁!颜色

不过,现在我需要有类似的默认字体属性(添加一个属性但有子特性,这些特性展开/折叠的,是这样的:

But, now I need to have something like the default Font property (add a property but then have sub-properties that are expandable/collapsible, like this:

目前,只有这样,我知道让一个自定义属性,在属性面板中VS是做这样的事情:

Currently, the only way I know to get a custom property in the Properties Pane in VS is to do something like this:

public Boolean isBaeltazorAwesome { get; set; }

这将显示在属性面板中的一个属性。但是,我需要这样的东西在下面的图片,在这里你可以展开Font属性,并得到一些更多的编辑子属性。

And that will show a single property in the properties pane. But I need something like in the picture below, where you can expand the Font property and get some more editable sub-properties.

如何才能做到这一点?

我知道找参考/异地资源是题外话,但如果你知道任何我想AP preciate它,如果你可以分享。我只是不知道如何搜索某些事情的时候,我不知道是干什么用的术语。那是怪异还是什么?

I know looking for references/off-site resources is "off-topic", but if you do know of any I would appreciate it if you could share. I just have no idea how to search for certain things when I don't know what terminology to use. Is that weird or what?

推荐答案

您可以尝试定义类型转换器属性自定义属性的类型声明:

You could try to define the TypeConverter attribute for your custom property's type declaration:

[TypeConverter(typeof(MyPropertyConverter))]
public struct MyProperty
{
    ...
}

public class MyPropertyConverter : TypeConverter
{
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, Object value, Attribute[] attributes)
    {
        PropertyDescriptorCollection collection = TypeDescriptor.GetProperties(typeof(MyProperty));
        // Reorganize the collection of sub-properties
        return collection;
    }

    // overrides of the methods: CanConvertTo, ConvertTo, CanConvertFrom, ConvertFrom etc
}

请参阅例如:Implementing类型转换器的Windows窗体