Quantcast
Channel: wpftoolkit Discussions Rss Feed
Viewing all articles
Browse latest Browse all 2157

New Post: Dynamic attribute to change DescriptionAttribute in PropertyGrid of CollectionEditor

$
0
0
Hi,

It is possible to do it.
The DescriptionAttribute has a public property "Description" which is ReadOnly, It also has a "DescriptionValue" whick is protected read/write. We can access this "DescriptionValue" property via Reflection. Here's how in C# :
Type myType = _propertyGrid.SelectedObject.GetType();
PropertyDescriptor descriptor = TypeDescriptor.GetProperties( myType )[ "Temperature" ];
DescriptionAttribute desc = descriptor.Attributes.OfType<DescriptionAttribute>().FirstOrDefault();
if( desc != null )
{
   PropertyInfo propertyInfo = desc.GetType().GetProperty( "DescriptionValue", BindingFlags.NonPublic | BindingFlags.Instance );
   propertyInfo.SetValue( desc, "Enter temperature (°F)", null );

   //A refresh on the PropertyGrid is needed to Regenerate the properties.
    object saveData = _propertyGrid.SelectedObject;
    _propertyGrid.SelectedObject = null;
    _propertyGrid.SelectedObject = saveData;
 }

Viewing all articles
Browse latest Browse all 2157

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>