Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The main issue is some PerformanceCounter properties such as CategoryName are marked with the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.readonlyattribute.aspx" rel="nofollow">ReadOnly</a> attribute. When the property grid sees a ReadOnly attribute on a property, you can't edit it, so you can' have the drop down with the list of category names.</p> <p>I don't know how what magic the designer uses to overcome this, but here is my solution. We add a custom <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.typedescriptionprovider.aspx" rel="nofollow">TypeDescriptionProvider</a> to the instance of PerformanceCounter edited, and provide a custom type descriptor that removes the ReadOnly limitation.</p> <p>This is how you can use it (with a propertyGrid1 instance of the PropertyGrid class):</p> <pre><code>PerformanceCounter counter = new PerformanceCounter(); // use a custom type description provider for this counter TypeDescriptor.AddProvider(new PerformanceCounterTypeProvider(), counter); // filter unwanted properties propertyGrid1.BrowsableAttributes = new AttributeCollection(DesignerSerializationVisibilityAttribute.Visible); // select it in the property grid propertyGrid1.SelectedObject = counter; </code></pre> <p>And this is the utility code used:</p> <pre><code>public class PerformanceCounterTypeProvider : TypeDescriptionProvider { private static PropertyDescriptor[] _properties; static PerformanceCounterTypeProvider() { List &lt; PropertyDescriptor&gt; properties = new List&lt;PropertyDescriptor&gt;(); foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(typeof(PerformanceCounter))) { PerformanceCounterPropertyDescriptor clone = new PerformanceCounterPropertyDescriptor(pd); properties.Add(clone); } _properties = properties.ToArray(); } public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) { return new PerformanceCounterTypeDescriptor(); } private class PerformanceCounterTypeDescriptor : CustomTypeDescriptor { public override PropertyDescriptorCollection GetProperties(Attribute[] attributes) { return new PropertyDescriptorCollection(PerformanceCounterTypeProvider._properties.ToArray()); } } private class PerformanceCounterPropertyDescriptor : PropertyDescriptor { private PropertyDescriptor _desc; public PerformanceCounterPropertyDescriptor(PropertyDescriptor desc) : base(desc, new List&lt;Attribute&gt;(desc.Attributes.OfType&lt;Attribute&gt;()).ToArray()) { _desc = desc; } public override void SetValue(object component, object value) { // we can't use _desc.SetValue because the underlying property descriptor checks it's read only ComponentType.GetProperty(Name).SetValue(component, value, null); } public override bool IsReadOnly { get { return false; } } public override bool CanResetValue(object component) { return _desc.CanResetValue(component); } public override Type ComponentType { get { return _desc.ComponentType; } } public override object GetValue(object component) { return _desc.GetValue(component); } public override Type PropertyType { get { return _desc.PropertyType; } } public override void ResetValue(object component) { _desc.ResetValue(component); } public override bool ShouldSerializeValue(object component) { return _desc.ShouldSerializeValue(component); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload