【发布时间】:2011-08-14 01:32:00
【问题描述】:
PropertyGrid... 对于用户我想只留下其中几个。但是现在我看到了所有内容,当看到诸如 Dock 或 Cursor 之类的东西时,用户会感到困惑...... 希望现在很清楚......
【问题讨论】:
标签: c# propertygrid
PropertyGrid... 对于用户我想只留下其中几个。但是现在我看到了所有内容,当看到诸如 Dock 或 Cursor 之类的东西时,用户会感到困惑...... 希望现在很清楚......
【问题讨论】:
标签: c# propertygrid
使用这个属性:
[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";
【讨论】:
要隐藏MyCtrl 属性,请在属性上使用[Browsable(False)] 属性。
[Browsable(false)]
public bool AProperty { get; set;}
要隐藏 inherited 属性,您需要覆盖 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#
【讨论】:
PropertyGrid 是设计时的事情,不是吗?
ControlDesigner 部分会导致表单设计崩溃或隐藏继承的属性会导致表单设计崩溃??