Databinding to PropertyInfo (static properties) in Silverlight

Databinding to PropertyInfo (static properties) in Silverlight

Old forum URL: forums.lhotka.net/forums/t/9003.aspx


skagen00 posted on Thursday, May 27, 2010

Maybe I'm missing something obvious, but I'm interested in databinding to properties of the PropertyInfo objects that I register.  Specifically, a localization key value (extended propertyinfo object) that I may run through a converter to essentially display a localized name for my property.

Trying to bind through the model does not seem to work - i.e. Model.FirstNameProperty.Name -- no dice.

Obviously the difference is that these are static members. I thought it might just work.

I haven't dug into this too deeply yet but I presume someone has run into this general problem before so I thought I would ask ahead of doing the digging.  It doesn't seem to work "automagically".

Chris

 

skagen00 replied on Thursday, May 27, 2010

Here's the avenue I'm taking. Maybe someone can tell me if I'm crazy.

First, introducing a class that exposes a a list of IPropertyInfo objects with a string indexer property - use the property name to get the IPropertyInfo.

     /// <summary>
    /// Provides for a list of properties to have an indexer property by property name.
    /// </summary>
    public class ManagedProperties
    {
        private List<IPropertyInfo> _propertyInfos = null;

        public IPropertyInfo this[string propertyName]
        {
            get
            {
                return _propertyInfos.First<IPropertyInfo>(x => x.Name == propertyName);
            }
        }

        public ManagedProperties(List<IPropertyInfo> properties)
        {
            _propertyInfos = properties;
        }
    }

Then, within my descendant of BusinessBase<T>:

  private static object _propertyInfoSyncRoot = new object();
        private static ManagedProperties _managedProperties = null;

        /// <summary>
        /// Gets an container of IPropertyInfo accessible with a property name indexer property.
        /// </summary>
        public ManagedProperties PropertyInfos
        {
            get
            {
                if (_managedProperties == null)
                {
                    lock (_propertyInfoSyncRoot)
                    {
                        if (_managedProperties == null)
                        {
                            _managedProperties = new ManagedProperties(this.FieldManager.GetRegisteredProperties());
                        }
                    }
                }
                return _managedProperties;
            }
        }

As a result one can do this in databinding then:

 Text="{Binding Model.PropertyInfos[AccountNumber], Converter={StaticResource BusinessLabels}}"

BusinessLabels being a value converter that takes a IPropertyInfo, and if it's an extended type, gets the localize key property of the extended property info and returns the localized value.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Copyright (c) Marimer LLC