Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Use Class As Property

Tags:

c#

I tried using class as a property but I can't change it's sub properties in the "Properties Tab"

I want to make a property like the Font property in this picture

like image 877
M4rsha11X Avatar asked May 04 '18 19:05

M4rsha11X


People also ask

What are class properties used for?

It is used to maintain a list of values in which the key is a string and the value is also a string i.e; it can be used to store and retrieve string type data from the properties file.

Can a class be a property C#?

Property in C# is a member of a class that provides a flexible mechanism for classes to expose private fields. Internally, C# properties are special methods called accessors.

What is a property in a class?

Properties are attributes or features that characterize classes. While classes are groups of objects, an instance is a specific object that actually belongs to a class.

How do you access the properties of a class?

Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property (where property is the name of the property). Static properties are accessed by using the :: (Double Colon): self::$property .


1 Answers

You need to decorate it with [TypeConverter(typeof(ExpandableObjectConverter))] to get the sub-properties to show up in the editor.

public struct MyStruct
{
    public int One;
    public int Two;
    public int Three;
}

public class MyEditableClass : Control
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public MyStruct MyProperty { get; set; } = new MyStruct();
}

The properties will now be expandable.

like image 179
Ron Beyer Avatar answered Oct 10 '22 19:10

Ron Beyer