This project is read-only.

PropertyGrid with custom and ordered categories not working

Apr 14, 2011 at 2:11 PM

Hi all,

I am a little bit frustrated because the PropertyGrid does not work correctly with my custom control. The custom control uses a TypeConverter to sort the properties and their categories in the PropertyGrid ([TypeConverter(typeof(SRPropertySorter))] ==> SRPropertySorter : ExpandableObjectConverter)

My situation is the following:

1.  Implementing a custom control: 


public class WinFormsTextbox : System.Windows.Forms.TextBox, ITextbox



        [SRCategory(SRKeys.CategoryAppearance), SRPropertyOrder(300)]


public new string Text {

  get { return base.Text; }

set {  base.Text = value.Contains(Environment.NewLine) ? value.Replace(Environment.NewLine, " ") : value; }




2. Implementing a CategoryAttribute extending the CategoryAttribute:

[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]    

public sealed class SRCategoryAttribute : System.ComponentModel.CategoryAttribute    {        

public SRCategoryAttribute(string category) : base(category) { }
        protected override string GetLocalizedString(string value) {

string category = AppResources.ResourceManager.GetString(value) ?? base.GetLocalizedString(value);
return category ?? value;        



3. Implenting a OrderAttribute:


public class SRPropertyOrderAttribute : Attribute {

private readonly int _Order;
public SRPropertyOrderAttribute(int order) { _Order = order; }
public int Order { get { return _Order; } } }

3. Use the SRCategoryAttribute  and the SRPropertyOrderAttribute  for the properties to sort:

[SRCategory(SRKeys.CategoryAppearance), SRPropertyOrder(300)]


public new string Text { ... }

4. Implementing the SRPropertySorter:

public class SRPropertySorter : ExpandableObjectConverter {
public override bool GetPropertiesSupported(ITypeDescriptorContext context) { return true; }
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) {

var pdc = TypeDescriptor.GetProperties(value, attributes);

var orderedProperties = new List<SRPropertyOrderPair>();
        foreach (PropertyDescriptor pd in pdc) { 

var attribute = pd.Attributes[typeof(SRPropertyOrderAttribute)];

var poa = (SRPropertyOrderAttribute)attribute;

orderedProperties.Add(new SRPropertyOrderPair(pd, attributes, poa.Order)); 

               orderedProperties.Add(new SRPropertyOrderPair(pd.Category, pd.Name, poa.Order)); 

return new PropertyDescriptorCollection((from SRPropertyOrderPair pop in orderedProperties where pdc[pop.Name] != null select pdc[pop.Name]).ToArray());


5. Use white to get the categories from the PropertyGrid:

var categories = _Window.Get<PropertyGrid>("propertyGrid").Categories;

6. Conclusion

==> The effect is that the Text property is identified as a Category because UIAutomation determines that it do not support the ValuePattern. 

class: White.Core.UIItems.PropertyGridItems.PropertyGrid

property: Categories:

var automationPatterns = new AutomationPatterns(element);

if (!automationPatterns.HasPattern(ValuePattern.Pattern))

categories.Add(new PropertyGridCategory(element, actionListener, finder));

I am getting the same effect even when I am using the System.ComponentModel.CategoryAttribute without sorting any property.


Does anybody know why this happen? I only want to get the same PropertyGrid structure from White as it is displayed in the UI. What I am doing wrong.

Thanks for all replies