First, We define a nested property class, which is named "Location". And it contains two properties: x, y
ref class Location
{
public:
float fX;
float fY;
[DisplayNameAttribute("X"),
DescriptionAttribute("X Position"),
DefaultValueAttribute(false)]
property float X
{
float get(void)
{
return fX;
}
void set(float fValue)
{
fX = fValue;
}
}
[DisplayNameAttribute("Y"),
DescriptionAttribute("Y Position"),
DefaultValueAttribute(false)]
property float Y
{
float get(void)
{
return fY;
}
void set(float fValue)
{
fY = fValue;
}
}
};
Then, we need a type converter to convert the nested property value to detailed value, and vice versa:
ref class LocConverter : public ExpandableObjectConverter
{
public:
virtual bool CanConvertFrom(ITypeDescriptorContext^ context, Type^ t) override
{
if (t == String::typeid) {
return true;
}
return ExpandableObjectConverter::CanConvertFrom(context, t);
}
virtual Object^ ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ info, Object^ value) override
{
if (value->GetType() == String::typeid) {
String^ s = (String^) value;
int comma = s->IndexOf(',');
if (comma != -1) {
Location^ qLoc = gcnew Location;
String^ qX = s->Substring(0, comma);
qLoc->fX = float::Parse(qX);
qX = s->Substring(comma+1);
qLoc->fY = float::Parse(qX);
return qLoc;
}
}
return ExpandableObjectConverter::ConvertFrom(context, info, value);
}
virtual Object^ ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destType) override
{
if (destType == String::typeid && value->GetType() == Location::typeid) {
Location^ qLoc = (Location^)value;
return qLoc->fX.ToString("F") + ", " + qLoc->fY.ToString("F");
}
return ExpandableObjectConverter::ConvertTo(context, culture, value, destType);
}
};
Final, we put the sub-nested property into main property menu:
ref class Property
{
public:
Location^ m_qLocation;
public:
Property(void)
{
m_qLocation = gcnew Location;
}
[TypeConverter((LocConverter::typeid)),
CategoryAttribute("Properties"),
DescriptionAttribute("Location"),
DefaultValueAttribute(false),
DisplayNameAttribute("Location")]
property Location^ Loc
{
Location^ get()
{
return m_qLocation;
}
void set(Location^ qType)
{
m_qLocation = qType;
}
}
};
請先 登入 以發表留言。