Here's a sample I just happend to write up today (what *else* would I
be doing on a sunday). Also, sorry for the formatting:
[TypeConverter(typeof(LocationConverter))]
public struct Location
{
public Location(int x, int y)
{
_X = x;
_Y = y;
}
int _X;
[NotifyParentProperty(true)]
public int X
{
get { return _X; }
set { _X = value; }
}
int _Y;
[NotifyParentProperty(true)]
public int Y
{
get { return _Y; }
set { _Y = value; }
}
}
public class LocationConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context,
Type destinationType)
{
if (destinationType == typeof(string) ||
destinationType == typeof(InstanceDescriptor))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override bool CanConvertFrom(ITypeDescriptorContext context,
Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
{
if (destinationType == typeof(string))
{
Location l = (Location)value;
return string.Format("{0},{1}", l.X, l.Y);
}
if (destinationType == typeof(InstanceDescriptor))
{
Type t = typeof(Location);
ConstructorInfo ci = t.GetConstructor(new Type[] {
typeof(int), typeof(int) });
Location l = (Location)value;
InstanceDescriptor d = new InstanceDescriptor(ci, new
object[] { l.X, l.Y });
return d;
}
return base.ConvertTo(context, culture, value,
destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
Location l = new Location();
string[] parts = value.ToString().Split(',');
l.X = Int32.Parse(parts[0]);
l.Y = Int32.Parse(parts[1]);
return l;
}
return base.ConvertFrom(context, culture, value);
}
}
Hi Shimon,
Thanks for your posting. As for the defining Custom Type Convertor
for
Nested Control property question, based on my experience, if your
Property's type class is normal class with some nested property
(other
class type which are also basic types), they'll automatically appear
in
VS.NET IDE's property window at design-time, and we can navigate
them
through the +/- flag. In addition, for the
========
I need it to look the way it look in datagrid - xml.
========
I'm not quite sure on this, would you provide some further
description
on
this? Generally, the TypeConverter's two main method:
ConvertTo, ConvertFrom can help us cast discretionary object to the
target
type (as long as the cast is valid).
Please feel free to post your further concerns here. Thanks,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers
no
rights.)
--------------------
| From: "Shimon Sim" <
[email protected]>
| Subject: How to write TypeConvert
| Date: Fri, 29 Jul 2005 12:26:37 -0400
| Lines: 13
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
| Message-ID: <#
[email protected]>
| Newsgroups:
microsoft.public.dotnet.framework.aspnet,microsoft.public.dotnet.fra
me
work.a
spnet.buildingcontrols
| NNTP-Posting-Host: ool-44c05922.dyn.optonline.net 68.192.89.34
| Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.buildingcontrols:3947
microsoft.public.dotnet.framework.aspnet:115183
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I am writing a control and need to introduce custom nested
property.
| I need it to look exactly as XXXStyle properties for DataGrid.
| I starting looking into TypeConverters and it seems that I need to
override
| ConvertTo and ConvertFrom methods. All the examples I saw show
some
custom
| implementation of value - comer separated or semicolon separated
values.
| I need it to look the way it look in datagrid - xml.
| Plus I have complex properties inside - like color and font.
| How do I handle all this?
|
| Thank you,
| Shimon.
|
|
|