Property grid and parametrized category descryption.

  • Thread starter usunto_bryjamus
  • Start date
U

usunto_bryjamus

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

Marc Gravell

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"; }
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

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

Marc Gravell

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
 

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

Top