PropertyGrid:隐藏基类属性,如何?

发布于 2024-11-29 14:25:29 字数 98 浏览 1 评论 0原文

PropertyGrid...对于用户我只想留下其中的几个。但现在我看到了所有,当用户看到像 Dock 或 Cursor 之类的东西时会感到困惑...... 希望现在已经清楚了...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒江雪… 2024-12-06 14:25:29

使用此属性:

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

对于继承的属性:

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

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

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; }
}

然后对于属性网格:

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

缺点是它将这些属性的访问级别从 更改

tx.PropertyA = "foo";

tx.ExtraProperties.PropertyA = "foo";

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";
夜还是长夜 2024-12-06 14:25:29

要隐藏 MyCtrl 属性,请使用该属性的 [Browsable(False)] 属性。

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

要隐藏继承属性,您需要覆盖基础属性并应用可浏览属性。

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

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

更好的方法是使用 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;
            }            
        }
    }
}

您可以将要隐藏的属性名称添加到 propertiesToHide 中,从而实现更清晰的分离。

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

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#

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文