I am creating custom winforms designer. I use label, textbox, combobox, listbox, button and checkbox on the designer. I don’t want to show all their properties on PropertyGrid
, so I created a custom attribute and set the grid to show only properties marked with that.
propertyGrid1.BrowsableAttributes = new AttributeCollection(
new MyAttribute()
);
Problem is, that now i have to create exactly same code for each of the controls. I have own wrapper class for every control i need, derived from the existing control.
class MyButton : Button
{
[MyAttribute]
[Category("Common properties")]
public new string Name { get { return base.Name; } set { base.Name = value; } }
}
class MyLabel : Label
{
[MyAttribute]
[Category("Common properties")]
public new string Name { get { return base.Name; } set { base.Name = value; } }
}
...
I have currently 7 properties from every control, that i always need to show.
The question
:
Is there a pattern that i can use to have just one class that has those properties and “inject” those somehow, instead of copy pasting same code for all the controls? I only have 6-8 controls, but i dislike the idea of having exactly same code written in many places.
Should I somehow find the properties I need and add my custom attribute ( and category ) to those at runtime? That would be “bit” hacky, but could work?
Using interface: I would still have to implement it, thus resulting same amount of code.
I can’t use generics: I am using original controls as base, because designer code handles them out of the box nicely and using generics causes a mess and lots of customization.
0
The ICustomTypeDescriptor interface implemented on your components will allow you to show only those properties you wish, or to inject properties on other objects. However, it is only for the property grid. It won’t help you with the API or code serialization.