xceedsoftware / wpftoolkit

All the controls missing in WPF. Over 1 million downloads.
Other
3.9k stars 877 forks source link

Allow PropertyGrid to use attributes from MetadataType #707

Open xceedsoftware opened 7 years ago

xceedsoftware commented 7 years ago

rgravesdg[CodePlex]
I propose a change to the PropertyGrid to allow the use of an associated MetadataType class to define attributes such as DisplayName. This is useful when dealing with auto generated partial classes.

The change is minor, but it does add a dependency on System.ComponentModel.DataAnnotations. The full file is attached, but the change is to use AssociatedMetadataTypeTypeDescriptionProvider to retrieve the PropertyDescriptors. protected static ListltPropertyDescriptorgt GetPropertyDescriptors( object instance ) { PropertyDescriptorCollection descriptors;

  TypeConverter tc = TypeDescriptor.GetConverter( instance );
  if( tc == null || !tc.GetPropertiesSupported() )
  {
    if (instance is ICustomTypeDescriptor)
      descriptors = ((ICustomTypeDescriptor)instance).GetProperties();
    else
      // descriptors = TypeDescriptor.GetProperties( instance.GetType() );
    {
      AssociatedMetadataTypeTypeDescriptionProvider provider = new AssociatedMetadataTypeTypeDescriptionProvider(instance.GetType());
      descriptors = provider.GetTypeDescriptor(instance).GetProperties();
    }
  }
  else
  {
    descriptors = tc.GetProperties( instance );
  }

  return ( descriptors != null )
    ? descriptors.CastltPropertyDescriptorgt().ToList()
    : null;
}
arielbh commented 6 years ago

It's exactly what I need. How about a PR?