PropertyGrid...对于用户,我只想留下其中的几个。但是现在我看到了所有的东西,当用户看到像Dock或Cursor之类的东西时会感到困惑。希望现在清楚了……
发布于 2011-08-14 09:35:33
使用此属性:
[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";
发布于 2011-08-14 09:39:58
若要隐藏MyCtrl
属性,请在该属性上使用[Browsable(False)]
属性。
[Browsable(false)]
public bool AProperty { get; set;}
要隐藏可浏览继承的属性,需要覆盖基本属性并应用可浏览属性。
[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#
https://stackoverflow.com/questions/7054408
复制相似问题