PropertyGrid:隐藏基类属性,怎么样?

7 c# propertygrid

PropertyGrid ...对于用户我想只留下几个.但是现在我看到了所有这些,当看到像Dock或Cursor这样的东西时,用户会感到困惑......希望现在很清楚......

Lar*_*ech 11

使用此属性:

[Browsable(false)]
public bool AProperty {...} 
Run Code Online (Sandbox Code Playgroud)

对于继承的属性:

[Browsable(false)]
public override bool AProperty {...} 
Run Code Online (Sandbox Code Playgroud)

另一个想法(因为你试图隐藏所有基类成员):

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; }
}
Run Code Online (Sandbox Code Playgroud)

然后为您的属性网格:

  MyCtrl tx = new MyCtrl();
  pg1.SelectedObject = tx.ExtraProperties;
Run Code Online (Sandbox Code Playgroud)

缺点是它会改变您对这些属性的访问级别

tx.PropertyA = "foo";
Run Code Online (Sandbox Code Playgroud)

tx.ExtraProperties.PropertyA = "foo";
Run Code Online (Sandbox Code Playgroud)


Mrc*_*ief 8

要隐藏MyCtrl属性,请在[Browsable(False)]属性上使用属性.

[Browsable(false)]
public bool AProperty { get; set;}
Run Code Online (Sandbox Code Playgroud)

要隐藏继承的 proeprties,您需要覆盖基数并应用browsable属性.

[Browsable(false)]
public override string InheritedProperty  { get; set;}
Run Code Online (Sandbox Code Playgroud)

注意:您可能需要根据具体情况添加virtualornew关键字.

更好的方法是使用a ControlDesigner.设计器有一个被称为覆盖的覆盖PreFilterProperties,可以用来为已经被提取的集合添加额外的属性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;
            }            
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以添加要隐藏的proeprties名称,propertiesToHide以便更清晰地分离.

信用到期:http://www.codeproject.com/KB/webforms/HidingProperties.aspx#