PropertyGrid + FileNameEditor + TypeConverter not working.
Jamie Seward 12 May 2004 15:04:22
I am having difficulty getting a FileNameEditor working with a Property that get/sets a custom Type (let's call it TextureNameType).
First here is what does work:
1) If I add the FileNameEditor attribute to a Property that get/sets a System::String everything works fine. I can open up the OpenFileDialog and it will change the Property as desired.
2) If I change the Property from a System::String to a TextureNameType (that has a fully operational TextureNameTypeConverter attribute) everything works fine. I can edit the field in the Property Grid and it will convert to and from a System::String properly.
The problem is when I try to give both attributes (FileNameEditor / TextureNameTypeConverter) to TextureNameType at the same time. The OpenFileDialog will open but selecting a file will bring up: "Invalid property value: Object type cannot be converted to target type."
I have set breakpoints in the TextureNameTypeConverter functions and it doesn't hit any of them. So it seems the PropertyGrid doesn't even check my TextureNameTypeConverter to see if it can convert the value returned by the FileNameEditor.
Does anyone know what is causing this? Thanks for any help!
Note: all of the classes are in a Managed C++ Class Library. The PropertyGrid is in a C# project.
Jamie Seward
=================================== This list is hosted by DevelopMentor® http://www.develop.com Some .NET courses you may be interested in:
Having used the propertygrid a fair bit I can only tell you what I know from experience.
When you append a class with a TypeConverter attribute the PropertyGrid uses the class specified to convert the value of the property, usually to a string. If there is set method on the property and no Editor attribute is specied it also uses the TypeConverter class to convert the input, also usually a string (it would be hard for it to be anything else).
If an Editor attribute is specified the PropertyGrid delegates all responsibility for altering the value of the property to the Editor using the return of the editor to set the property, not bothering to look at the TypeConverter attribute because it assumes that the Editor knows whats it's doing and returning an object of the right type. This is obviously what is causing the exception.
What I suggest you do is to derive from FileNameEditor thusly
public class TextureNameTypeEditor : FileNameEditor { public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { object obj = base.EditValue(context, provider, value); TextureNameType tnt = TextureNameType.Convert(obj); // or whatever you need to do return tnt; } }
Then add an [Editor(typeof(TextureNameTypeEditor))] to your property.
Chris
-----Original Message-----> From: Jamie Seward [mailto:jseward@IRONCLADGAMES.COM] > Sent: Wednesday, 12 May 2004 10:04 PM> To: DOTNET-CLR@DISCUSS.DEVELOP.COM> Subject: [DOTNET-CLR] PropertyGrid + FileNameEditor + > TypeConverter not working.>
I am having difficulty getting a FileNameEditor working with > a Property that get/sets a custom Type (let's call it > TextureNameType).>
First here is what does work:>
1) If I add the FileNameEditor attribute to a Property that > get/sets a System::String everything works fine. I can open > up the OpenFileDialog and it will change the Property as desired.>
2) If I change the Property from a System::String to a > TextureNameType (that has a fully operational > TextureNameTypeConverter attribute) everything works fine. I > can edit the field in the Property Grid and it will convert > to and from a System::String properly.>
The problem is when I try to give both attributes > (FileNameEditor / TextureNameTypeConverter) to > TextureNameType at the same time. The OpenFileDialog will > open but selecting a file will bring up: "Invalid property > value: Object type cannot be converted to target type.">
I have set breakpoints in the TextureNameTypeConverter > functions and it doesn't hit any of them. So it seems the > PropertyGrid doesn't even check my TextureNameTypeConverter > to see if it can convert the value returned by the FileNameEditor.>
Does anyone know what is causing this? Thanks for any help!>
Note: all of the classes are in a Managed C++ Class Library. > The PropertyGrid is in a C# project.>
Jamie Seward>
===================================> This list is hosted by DevelopMentor(r) http://www.develop.com > Some .NET courses you may be interested in:>