[英]PropertyGrid: Hide base class properties, how?
PropertyGrid... 对于用户我想只留下其中几个。 但是现在我看到了所有内容,当看到诸如 Dock 或 Cursor 之类的东西时,用户会感到困惑......希望现在很清楚......
使用此属性:
[Browsable(false)]
public bool AProperty {...}
对于继承的属性:
[Browsable(false)]
public override bool AProperty {...}
另一个想法(因为您试图隐藏所有基本 class 成员):
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";
要隐藏MyCtrl
属性,请在属性上使用[Browsable(False)]
属性。
[Browsable(false)]
public bool AProperty { get; set;}
要隐藏继承的属性,您需要覆盖 base 并应用 browsable 属性。
[Browsable(false)]
public override string InheritedProperty { get; set;}
注意:您可能需要根据具体情况添加virtual
或new
关键字。
更好的方法是使用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#
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.