Hi,
Is it possible to parametrize Property grid category?
I have class:
[DefaultProperty("Name")]
public class Test
{
private string name;
[Category("Misc"),
Description("Name")]
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
I need to parametrize line where I set category for Name property (
Category(<<my parameter>>) ). I prepare app where I want to change this
string using configuration file for this e.g. xml file where I have
category string in different languages.

Signature
Best regards,
Klaudiusz
Marc Gravell - 07 Jan 2008 14:41 GMT
> I prepare app where I want to change this string using configuration
> file for this e.g. xml file where I have category string in
> different languages.
Not with CategoryAttribute itself... but IIRC you can subclass it, and
override GetLocalizedString to lookup from your language file
(suggest: resx, since it already handles languages etc).
public class MyCategoryAttribute : CategoryAttribute {
public MyCategoryAttribute(string category) : base(category) { }
protected override string GetLocalizedString(string key) {
return YourLookupHere(key);
}
}
class SomeClass {
[MyCategory("LOOKUP_KEY")]
string SomeProperty {
get { return "abc"; }
}
}
Robbe Morris - [MVP] C# - 07 Jan 2008 15:54 GMT
You might want to look at this approach for using the .NET
PropertyGrid. Much simpler and easier to manipulate
at runtime.
http://www.eggheadcafe.com/tutorials/aspnet/270e9432-d236-47e7-b1af-5cd3abe27a75
/net-propertygrid-control.aspx

Signature
Robbe Morris [Microsoft MVP - Visual C#]
AdvancedXL Server, Designer, and Data Analyzer
Convert cell ranges in Excel to rule driven web surveys
Free download: http://www.equalssolved.com/default.aspx
> Hi,
>
[quoted text clipped - 27 lines]
> string using configuration file for this e.g. xml file where I have
> category string in different languages.
Nicholas Paldino [.NET/C# MVP] - 07 Jan 2008 16:04 GMT
You should have your class implement the ICustomTypeDescriptor
interface, which returns the properties (as well as the attributes
associated with the properties). When the PropertyGrid sees this, it will
query that implementation for the type information, instead of going through
reflection. You can then return the Category attributes with whatever value
you want.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Hi,
>
[quoted text clipped - 27 lines]
> string using configuration file for this e.g. xml file where I have
> category string in different languages.
Marc Gravell - 07 Jan 2008 22:37 GMT
> You should have your class implement the ICustomTypeDescriptor
> interface
I'm no stranger to System.ComponentModel, but that seems a little
excessive just for this? Arguably, though, it could be a reasonable
job for a TypeDescriptionProvider (especially if you don't have
control of the class and want to change the attributes to add
localization support to an object that doesn't provide it itself) -
but forcing the class to implement this (non-trivial) interface? Do-
able, I suppose, but arguably not very clean...?
I'm not saying it is the de-facto response, but MS do it via the
"subclass CategoryAttribute" approach... IIRC, SRCategoryAttribute,
XmlCategoryAttribute and a whole bunch of others to meet different
scenarios. There is good support for this, and it is trivial to
implement...
I'm just saying...
Marc