What design pattern is used to provide additional class properties?

When you have a TableLayoutPanel on your Form and you drag a Label to a cell, several properties are available in the Label control. I think the same construct is used when you drag a Tooltip element on a form.

I would like to know which design template is used to achieve this. Is this a decorator template?

+4
source share
2 answers

What you see is called Extension Providers .

For example, when a ToolTip component is added to a form, it provides a property called ToolTip for each control in that form. A tooltip property appears in any attached PropertyGrid control. http://msdn.microsoft.com/en-us/library/ms171836.aspx

I can’t come up with a well-known template that describes how they work, exactly, but the mechanism is simple.

You must implement IExtenderProvider . The WinForms designer will call CanExtend for every other control on the surface, and your extender can indicate whether it provides additional attributes for each control.

 public interface IExtenderProvider { bool CanExtend(object extendee); } 

Valid attributes that other controls will extend are declared using the ProvidePropertyAttribute and method to provide the value .

+7
source

No, this is not achieved using the design pattern. These properties are just public properties set by the control, these properties are added to the control through inheritance, i.e. Subclasses of Control . The visual studio designer checks the class that implements these controls to determine the properties that they expose, and then provides a user interface for customizing them.

+1
source

Source: https://habr.com/ru/post/1389224/


All Articles