Customize the properties of the property (Attribute): (3)

zhaozj2021-02-16  49

2, unfolded form

Expanded forms are used for a property for our custom class, such as we define a class, one of the properties in this class is another class we defined. In this case, the attribute browser is not a way to convert, so it is displayed as an incorporate content. If we want to edit this property in the form of expansion, we need to rewrite the property converter as the upper surface.

We first define a class of its own class as a future attribute type. The specific code is as follows:

Public Class Expandproperty

{

Private int _intlist = 0;

Public int Int Intlist

{

Get {return this._intlist;

Set {this._intlist = value;

}

Private string _strlist = "null";

Public String Strlist

{

Get {returnid ._strlist;

Set {this._strlist = value;

}

}

Then we declare a type of properties in another class, where we do not add any nature limitations, the property browser cannot be converted to change the properties. The code that implements this attribute is as follows:

Private expandproperty _droplist;

[CategoryAttribute ("Custom complex type settings (including custom type converters)",

TypeConvertRibute (typeof (propertyGridApp.expandconvert),

ReadonlyAttribute (FALSE)]

Public ExpandProperty Droplist

{

Get {return this._droplist;

Set {this._droplist = value;

}

In order to allow the property browser to edit this property, that is to say, it is necessary to convert the property into a string, and can be converted from a string to the class: we must write the following code:

///

/ / / Can expand type converter

/// Expandproperty

///

Public class expandconverter: system.componentmodel.expandableObjectConverter

{

Public expandconvert ()

{

}

///

/// Override this method has determined whether the attribute can be converted

///

Public Override Bool CanconvertTo (System.comPonentModel.ityped, System.Type DestinationType)

{

IF (destinationType == typeof (propertyGridApp.expandproperty))

Return True;

Return Base.canconvertTo (Context, DestinationType);

}

///

/// Overwrite this method and make sure the DestinationType parameter is a string, then formatted the displayed content

///

Public Override Object ConvertTtto (System.comPonentModel.ityped, System.globalization.cultureInfo Culture, Object Value, System.Type DestinationType) {

IF (destinationType == TypeOf (system.string) && value isottygridapp.expandproperty)

{

PropertyGridApp.expandproperty Source = (PropertyGridApp.expandproperty) Value;

Return Source.Ist "," Source.Strlist;

}

Return Base.ConvertTo (Context, Culture, Value, DestinationType);

}

///

/// Override this method that is determined that the input string can be transformed

///

Public override Bool CanconvertFrom (System.comPonentModel.ityped, System.Type SourceType)

{

IF (SourceType == TypeOf (String))

Return True;

Return Base.canconvertFrom (Context, SourceType);

}

///

/// Override this method to convert the entered string into classes according to the conversion format of the ConvertTo () method, and return to this class

///

Public Override Object ConvertFrom (System.comPonentModel.ityped, System.globalization.cultureInfo Culture, Object Value)

{

Value Is String

{

String s = (string) Value;

INT COMMA = S.Indexof (",");

IF (COMMA! = - 1)

{

Try

{

String Intlist = s.substring (0, COMMA);

String strlist = s.substring (COMMA 1, S.LENGTH-COMMA-1);

Propertygridapp.expandproperty EP = New ExpandProperty ();

EP.Intlist = int.parse (intlist);

EP.STRLIST = Strlist;

Return EP;

}

Catch

{

Return Base.ConvertFrom (Context, Culture, Value);

}

}

}

Return Base.ConvertFrom (Context, Culture, Value);

}

}

The picture after compilation is as follows:

转载请注明原文地址:https://www.9cbs.com/read-28327.html

New Post(0)