【问题标题】:PropertyGrid: Hide base class properties, how?PropertyGrid:隐藏基类属性,如何?
【发布时间】:2011-08-14 01:32:00
【问题描述】:

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

【问题讨论】:

    标签: c# propertygrid


    【解决方案1】:

    使用这个属性:

    [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";
    

    【讨论】:

    • 这不是继承类,我想隐藏基类的属性。在这种情况下:TextBox 道具。如何隐藏基类道具?就是这个问题。
    • 是的,它有效,我知道。您会在我的问题中看到以下陈述:“在这种情况下适用什么技术?隐藏?基类的每个道具?”...我只是想知道也许我可以过滤属性...但似乎在运行时它不是可能,因为所有属性对于已编译的程序集都是静态的。
    • ExtraProperties 的想法具有使序列化更简单的额外优势。
    【解决方案2】:

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

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

    要隐藏 inherited 属性,您需要覆盖 base 并应用 browsable 属性。

    [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#

    【讨论】:

    • 没错.. 谢谢。什么意思 ("{0}:MyControl>") ?它来自 ASP 吗?我正在使用 WinForms...
    • 是的。对于 Winforms,您可以忽略该部分。我会更新我的答案。
    • 运行时是什么意思? PropertyGrid 是设计时的事情,不是吗?
    • 没有。有 PropertyGrid 控件……我什至在设计时尝试过,它使表单设计崩溃……没有帮助,抱歉……
    • 您的意思是ControlDesigner 部分会导致表单设计崩溃或隐藏继承的属性会导致表单设计崩溃??
    猜你喜欢
    • 2013-09-26
    • 1970-01-01
    • 2012-07-01
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    相关资源
    最近更新 更多