开发者

PropertyGrid: Hide base class properties, how?

PropertyGrid... for users Id like to leave only several of them. But now I see all, and us开发者_Python百科ers would be confused when see something like Dock or Cursor and such... Hope it's clear for now...


Use this attribute:

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

For the inherited properties:

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

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

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 down side is it changes your access level of those properties from

tx.PropertyA = "foo";

to

tx.ExtraProperties.PropertyA = "foo";


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

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

To hide inherited proeprties, you need to override the base and apply the browsable attribute.

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

Note: You may need to add the virtual or new keyword depending on the circumstances.

A better approach would be to use a ControlDesigner. The designer has an override called PreFilterProperties that can be used to add extra attributes to the collection that has been extracted by 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 proeprties you wish to hide to propertiesToHide which allows for a cleaner separation.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜