How to write TypeConvert

S

Shimon Sim

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.
 
K

Kevin Yu [MSFT]

Hi Shimon,

We have reviewed this issue and are currently researching on it. We will
update you ASAP. Thanks for your patience!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
S

Steven Cheng[MSFT]

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.framework.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.
|
|
|
 
B

Brock Allen

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);
}
}
 
S

Shimon Sim

Thank a lot.
I saw similar code in the MSDN example - can't understand one thing - there
this 'value' comes from? How is it stored in the page?

Thanks
Shimon.

Brock Allen said:
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.frame
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.
|
|
|
 
B

Brock Allen

TypeConverters are used when the developer is chaning the data in the property
page for a property of your contorl. The property page is a text entry form
-- there needs to be some way to take the text and convert it to the strongly
typed property on your control. So the value is passed in by VS.NET from
what the user types in the property page.




Thank a lot.
I saw similar code in the MSDN example - can't understand one thing -
there
this 'value' comes from? How is it stored in the page?
Thanks
Shimon.
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.
|
|
|
 
T

Teemu Keiski

Hi,

it doesn't need a type converter (it is meant for converting complex object
to string so that it can also be parsed back from markup to represent a
property).

Instead you make the style property read-only, and apply

[NotifyParentProperty(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty)]

attributes to the property.

The style property could look something like this

[NotifyParentProperty(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty)]
public virtual TableItemStyle ItemStyle
{
get
{
if (this.itemStyle == null)
{
this.itemStyle = new TableItemStyle();
if (base.IsTrackingViewState)
{
this.itemStyle.TrackViewState();
}
}
return this.itemStyle;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top