Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Property grid and parametrized category descryption.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
usunto_bryjamus@interia.pl - 07 Jan 2008 14:20 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.