none
Localize Properties for the PropertyGrid

    Question

  • I would like to localize properties of an object to show them in a PropertyGrid.

    1. With DisplayNameAttribute I am able to set a static display name for a property. But I cannot use this attribute for localization. It does not support a GetLocalizedString like the CategoryAttribute.
       
    2. I tried to implement the ICustomTypeDescriptor in my UserControl. But the PropertyGrid shows the message "Object reference not set to an instance of an object." in the value column of all localized properties. I am not able to find my flaw

      I think the bug is in this code fragment:

      public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
      {
        PropertyDescriptorCollection props = new PropertyDescriptorCollection(null);
       
      foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(this, attributes, true))
        {
         
      string name = Resources.ResourceManager.GetString(prop.Name);
         
      if (String.IsNullOrEmpty(name)) { name = prop.Name; }

         
      Attribute[] attrs = new Attribute[prop.Attributes.Count];
          prop.Attributes.CopyTo(attrs, 0);

         
      PropertyDescriptor pnew = TypeDescriptor.CreateProperty(this.GetType(), name,
            prop.PropertyType, attrs);
          props.Add(pnew);
        }
        return props;
      }

    How are I am able to localize the display name of properties? 

    Monday, September 04, 2006 11:31 AM

Answers

  • Rather than creating a custom type descriptor you should follow the technique used by .NET itself.  MS created custom attributes, deriving from the base attribute, that allow them to specify the name of a resource string and load it dynamically rather than relying on the built-in versions.

    public class SRDescriptionAttribute : DescriptionAttribute
    {
       public SRDescriptionAttribute ( string resourceName )
       { }

       public override string Description
       {
          get 
          {
             if not loaded yet
                load string from resource
           }
        }
    }

    Note that you shouldn't localize the display name anyway.  The display name is what the user would type in as the member name.  This isn't a localized value.

    Michael Taylor - 9/4/06

    Monday, September 04, 2006 9:54 PM
    Moderator

All replies

  • Rather than creating a custom type descriptor you should follow the technique used by .NET itself.  MS created custom attributes, deriving from the base attribute, that allow them to specify the name of a resource string and load it dynamically rather than relying on the built-in versions.

    public class SRDescriptionAttribute : DescriptionAttribute
    {
       public SRDescriptionAttribute ( string resourceName )
       { }

       public override string Description
       {
          get 
          {
             if not loaded yet
                load string from resource
           }
        }
    }

    Note that you shouldn't localize the display name anyway.  The display name is what the user would type in as the member name.  This isn't a localized value.

    Michael Taylor - 9/4/06

    Monday, September 04, 2006 9:54 PM
    Moderator
  • Hi,

    You can follow the technique that Michael explained and I see no problem with localizing the DisplayName. The default DisplayName displays the name of the member. It is far more user friendly to have a descriptive name with spaces.

    The .Net framework contains a private class called SRDisplayNameAttribute. Here is the important method for you:
    public override string get_DisplayName()
    {
    if (!this.replaced)
    {
    this.replaced = true;
    base.DisplayNameValue = SR.GetString(base.DisplayName);
    }
    return base.DisplayName;
    }

    If your localization mechanism is not based on resources, you may also play with PropertyDescriptors. Several articles on CodeProject will tell you more. Two of them are
    this one and this one.

    I hope this helps your issue with the PropertyGrid.

    Best regards,

    Nicolas Cadilhac
    VisualHint - Home of Smart PropertyGrid.Net
    PropertyGrid forums

    Tuesday, September 05, 2006 1:16 PM