My Blog

WinForms Filtered PropertyGrid

by lupok on mercoledì 17 marzo 2021 22:07

This is the type converter to be applied to the class assigned to the PropertyGrid.

public class FilterPropertyTypeConverter : TypeConverter
   {
      private FilteredPropertyGrid.PropertyGrid m_filterPropertyGrid;
 
      public FilterPropertyTypeConverter()
      {
 
      }
 
      public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
      {
         var properties = TypeDescriptor.GetProperties(value);
 
         if (m_filterPropertyGrid == null || String.IsNullOrEmpty(m_filterPropertyGrid.FilterProperties))
            return properties;
 
         List<PropertyDescriptor> propertyDescriptors = new List<PropertyDescriptor>();
 
         foreach (PropertyDescriptor property in properties)
         {
            if (!property.IsBrowsable)
               continue;
 
            if (Convert.ToString(property.DisplayName).ToLower().Contains(m_filterPropertyGrid.FilterProperties.ToLower()))
               propertyDescriptors.Add(property);
         }
 
         PropertyDescriptorCollection propertyDescriptorCollection = new PropertyDescriptorCollection(propertyDescriptors.ToArray());
 
         return propertyDescriptorCollection;
      }
 
      public override bool GetPropertiesSupported(ITypeDescriptorContext context)
      {
         var result = base.GetPropertiesSupported(context);
 
         var pi = context.GetType().GetProperty("OwnerGrid");
 
         if (pi == null)
            return result;
 
         var propertyGrid = pi.GetValue(context) as FilteredPropertyGrid.PropertyGrid;
 
         if (propertyGrid == null)
            return result;
 
         m_filterPropertyGrid = propertyGrid;
 
         return !String.IsNullOrEmpty(m_filterPropertyGrid.FilterProperties);
      }
   }


FilteredPropertyGrid.zip

Blogs Parent Separator My Blog
PropertyGrid
FilteredPropertyGrid
Filtered
Author
lupok

My Blog

Tags