PropertyGrid: hide base class properties, how?

PropertyGrid ... for Id users like to leave only a few of them. But now I see everything, and users will be confused when they see something like Dock or Cursor, etc. Hope this is clear now ...

+6
source share
2 answers

Use this attribute:

[Browsable(false)] public bool AProperty {...} 

For inherited properties:

 [Browsable(false)] public override bool AProperty {...} 

Another idea (since you are trying to hide all base classes):

 public class MyCtrl : TextBox { private ExtraProperties _extraProps = new ExtraProperties(); public ExtraProperties ExtraProperties { get { return _extraProps; } set { _extraProps = value; } } } public class ExtraProperties { private string _PropertyA = string.Empty; [Category("Text Properties"), Description("Value for Property A")] public string PropertyA {get; set;} [Category("Text Properties"), Description("Value for Property B")] public string PropertyB { get; set; } } 

and then for your property grid:

  MyCtrl tx = new MyCtrl(); pg1.SelectedObject = tx.ExtraProperties; 

The downside changes the access level of these properties from

 tx.PropertyA = "foo"; 

to

 tx.ExtraProperties.PropertyA = "foo"; 
+9
source

To hide MyCtrl properties, use the [Browsable(False)] attribute for the property.

 [Browsable(false)] public bool AProperty { get; set;} 

To hide the inherited proeprties, you need to redefine the base and apply the view attribute.

 [Browsable(false)] public override string InheritedProperty { get; set;} 

Note. You may need to add the virtual or new keyword, as appropriate.

A better approach would be to use a ControlDesigner . The designer has an override called PreFilterProperties , which can be used to add additional attributes to the collection that was retrieved using the PropertyGrid .

 Designer(typeof(MyControlDesigner))] public class MyControl : TextBox { // ... } public class MyControlDesigner : ... { // ... protected override void PreFilterProperties( IDictionary properties) { base.PreFilterProperties (properties); // add the names of proeprties you wish to hide string[] propertiesToHide = {"MyProperty", "ErrorMessage"}; foreach(string propname in propertiesToHide) { prop = (PropertyDescriptor)properties[propname]; if(prop!=null) { AttributeCollection runtimeAttributes = prop.Attributes; // make a copy of the original attributes // but make room for one extra attribute Attribute[] attrs = new Attribute[runtimeAttributes.Count + 1]; runtimeAttributes.CopyTo(attrs, 0); attrs[runtimeAttributes.Count] = new BrowsableAttribute(false); prop = TypeDescriptor.CreateProperty(this.GetType(), propname, prop.PropertyType,attrs); properties[propname] = prop; } } } } 

You can add the names of the proeprties you want to hide up to propertiesToHide , which allows a cleaner separation.

Credit provided: http://www.codeproject.com/KB/webforms/HidingProperties.aspx#

+6
source

Source: https://habr.com/ru/post/894999/


All Articles