TypeConverter
In this series of the previous article, and everyone accused of the control of the development and PropertyGrid relationship, I do not know now we have a more comprehensive understanding of PropertyGrid, do not know whether we have to do a project, the PropertyGrid pull into tinker?
Reinvent
Now let's think about a problem: fake, PropertyGrid does not divide the attributes and events into two pagination to show, what will be the effect?
That's a bit of a mess.
So if you design a control that has a lot of properties, and some of the correlations are strong, or they're all operational, then we can categorize them and put them together.
We can specify the following attribute for this control class:
[PropertyTab(typeof(YourPropertyTab), PropertyTabScope.Component)]
public class YourControlClass
{
}
Where, the previous parameter specifies the class to process the PropertyTab, and the latter indicates when to apply the component for the current component, the document is dedicated to the current documentation, and the global can only be explicitly removed by the parent, and static cannot be removed.
Internal class Yourpropertytab:propertytab
{
internal Yourcontroltype target;
public override string TabName
{
Get
{
return "tab name";
}
}
public override Bitmap Bitmap
{
Get
{
return to New Bitmap (base. Bitmap, New Size (16,16);//This is a. bmp file with the same name as the Yourpropertytab class that is saved as an embedded resource
}
}
public override bool CanExtend (object o)
{
return O is yourcontroltype;//when to use this tab
}
public override PropertyDescriptorCollection GetProperties (object component, attribute[] attrs) {
return GetProperties (NULL, component, ATTRS);
}
/**////the main logic. Here defines how to implement the tab display
public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object component, Attr Ibute[] attrs)
{
Yourcontroltype UC = component as Yourcontroltype;
if (UC = = null)
{
//The following code implementation is not yourcontroltype when using the logic of its own type.
TypeConverter TC = typedescriptor.getconverter (component);
if (TC!= NULL)
{
return TC. GetProperties (context, component, attrs);
}
Else
{
return Typedescriptor.getproperties (component, attrs);
}
}
target = UC;
ArrayList proplist = new ArrayList ();
//.. Create a property list
Proplist.add (The new Yourpropertydescriptor (this);
propertydescriptor[] props = (propertydescriptor[]) Proplist.toarray (typeof (PropertyDescriptor));
return new propertydescriptorcollection (props);
}
//We also need to create custom PropertyDescriptor for GetProperties methods to use.
Private Class Yourpropertydescriptor:propertydescriptor
{
Yourpropertytab owner;
Public Numpointspropertydescriptor (yourpropertytab owner)://Note the parameters here
Base ("PropertyName", New Attribute[]{categoryattribute.data, Refreshpropertiesattribute.all})//The second parameter is when the specified property changes, and//Other attributes of the linkage, the entire property page is refreshed, all-refresh, default-not, repaint-redraw Properties Window
{
This.owner = owner;
}
the type of the public override type propertytype//property
{
Get
{
return typeof (int);
}
}
What type of
Property association object is
public override Type ComponentType
{
Get
{
return typeof (Yourcontroltype);
}
}
public override bool IsReadOnly {Get{return false;}
public override Object GetValue (object o)//related to what properties of the associated object
{
Return ((Yourcontroltype) o). Proterty_1;
}
public override void SetValue (object o, Object value)//related to what attributes of the associated object
{
Yourcontroltype UC = o as Yourcontroltype;
UC. Property_1 = (int) value;
}
public override void Resetvalue (object o) {}//words too literally
public override bool Canresetvalue (object o)//words too literally
{
return false;
}
/**////does This property participate in code generation?
public override bool Shouldserializevalue (object o)
{
return false;
}
}
}