PropertyGrid localization

Grids for WPF Forum

Posted 10 years ago by Sasha
Version: 14.1.0601
Avatar

Hi,

I define PropertyGrid in XAML next way: 

<propgrid:PropertyGrid  x:Name="PropertiesUserControl"  IsSummaryVisible="True" AreNestedCategoriesSupported="True" 
                        IsEnabled="True" AreDefaultSortDescriptionsEnabled="False"  
                        AreCategoriesAutoExpanded="False" SelectedObject="{Binding SelectedObject}"
                        IsAsynchronous="True">
</propgrid:PropertyGrid>

 This way I define property to be shown in PropertyGrid:

[LocalizedCategory("Category"), 
 LocalizedDisplayName("DisplaySomeProperty"), 
 LocalizedDescription("DescSomeProperty")]
public double SomeProperty
{
    get { return _someProperty; }
    set
    {
        _someProperty = value;
        RaisePropertyChanged("SomeProperty");
    }
}

 where LocalizedDisplayName class is:

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event)]
    sealed class LocalizedDisplayNameAttribute : DisplayNameAttribute
    {
        static string Localize(string key)
        {
            string resourceValue = Application.Current.Resources[key].ToString();
            try
            {
                return resourceValue;
            }
            catch (Exception)
            {
                return key;
            }
        }
 
        public LocalizedDisplayNameAttribute(string key)
            : base(Localize(key))
        {
        }
    }

 It's loading localized properties, but when I change a language on runtime, properties in PropertyGrid are not localized.

On changing language I have such line:

PropertiesUserControl.Refresh();

 But properties are not refreshed. When I had a look at ItemsSource in debug mode I found out that Accessors of each PropertyDescriptorDataAccessor are not refreshed and have old localized values. How can I refresh them?

Thank you.

Comments (1)

Answer - Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Sasha,

I believe that .NET reflection will only load that attribute once and use its value from that point on.  You could probably change this if you override the DisplayNameAttribute.DisplayName property and return the localized value each time.  Of course that could affect performance since now you aren't caching the value.  So it could be better to cache it until some global static variable is changed (perhaps a version counter of sorts for changes to the current culture).


Actipro Software Support

The latest build of this product (v24.1.3) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.