Custom Property with Sub-Properties in Visual Stud

2019-07-17 17:14发布

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:

Appearance

  • BackColor
  • ForeColor
  • Font
    • Font Family
    • Font Size, etc etc.

I can already do this:

Appearance

  • BackColor
  • ForeColor
  • Hooray!Color

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:

enter image description here

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; }

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.

How can this be done?

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?

1条回答
我只想做你的唯一
2楼-- · 2019-07-17 17:41

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
}

See example: Implementing TypeConverter for Windows Forms

查看更多
登录 后发表回答