In the previous articles, we added a complex type Scope for the control and provided a type converter for its type. Now we can edit its value in the property browser, and its value is serialized in the source code. But have you found that it is not convenient to edit the attribute value in the property browser. Because the attribute is only "10, 200" Therefore, you must modify the format according to this format. If a format error occurs, an exception is thrown. For example, 10200" . We expect that each sub-attribute of this attribute can be independently edited. This is not impossible and the implementation is quite simple.
To independently edit sub-attributes in the property browser, We need to rewrite two methods: GetPropertiesSupported () and GetProperties (). The complete code of ScopeConverter is as follows:
Public class ScopeConverter: TypeConverter
{
Public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
{
If (sourceType = typeof (String) return true;
Return base. CanConvertFrom (context, sourceType );
}
Public override bool canconvertion (ITypeDescriptorContext context, Type destinationType)
{
If (destinationType = typeof (String) return true;
If (destinationType = typeof (InstanceDescriptor) return true;
Return base. canconvertize (context, destinationType );
}
Public override object convertion (ITypeDescriptorContext context, System. Globalization. CultureInfo culture, object value, Type destinationType)
{
String result = "";
If (destinationType = typeof (String ))
{
Scope scope = (Scope) value;
Result = scope. Min. ToString () + "," + scope. Max. ToString ();
Return result;
}
If (destinationType = typeof (InstanceDescriptor ))
{
ConstructorInfo ci = typeof (Scope). GetConstructor (new Type [] {typeof (Int32), typeof (Int32 )});
Scope scope = (Scope) value;
Return new InstanceDescriptor (ci, new object [] {scope. Min, scope. Max });
}
Return base. convertize (context, culture, value, destinationType );
}
Public override object ConvertFrom (ITypeDescriptorContext context, System. Globalization. CultureInfo culture, object value)
{
If (value is string)
{
String [] v = (String) value). Split (',');
If (v. GetLength (0 )! = 2)
{
Throw new ArgumentException ("Invalid parameter format ");
}
Scope csf = new Scope ();
Csf. Min = Convert. ToInt32 (v [0]);
Csf. Max = Convert. ToInt32 (v [1]);
Return csf;
}
Return base. ConvertFrom (context, culture, value );
}
Public override bool GetPropertiesSupported (ITypeDescriptorContext context)
{
Return true;
}
Public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Attribute [] attributes)
{
Return TypeDescriptor. GetProperties (typeof (Scope), attributes );
}
}
In the GetProperties method, I use TypeDescriptor to obtain all attribute descriptors of the Scope class and return them. If you are not familiar with TypeDescriptor, refer to MSDN.
After the two methods are rewritten and compiled, you can view the properties of the control in the test project. You can see that the Scope is in the following form: