propertygrid1

更改属性的显示方式

  要更改某些属性的显示方式,您可以对这些属性应用不同的特性。特性是用于为类型、字段、方法和属性等编程元素添加批注的声明标记,在运行时可以使用反射对其进行检索。下面列出了其中的一部分:
  • DescriptionAttribute - 设置显示在属性下方说明帮助窗格中的属性文本。这是一种为活动属性(即具有焦点的属性)提供帮助文本的有效方法。可以将此特性应用于 MaxRepeatRate 属性。
  • CategoryAttribute - 设置属性在网格中所属的类别。当您需要将属性按类别名称分组时,此特性非常有用。如果没有为属性指定类别,该属性将被分配给杂项类别。可以将此特性应用于所有属性。
  • BrowsableAttribute – 表示是否在网格中显示属性。此特性可用于在网格中隐藏属性。默认情况下,公共属性始终显示在网格中。可以将此特性应用于 SettingsChanged 属性。
  • ReadOnlyAttribute – 表示属性是否为只读。此特性可用于禁止在网格中编辑属性。默认情况下,带有 get 和 set 访问函数的公共属性在网格中是可以编辑的。可以将此特性应用于 AppVersion 属性。
  • DefaultValueAttribute – 表示属性的默认值。如果希望为属性提供默认值,然后确定该属性值是否与默认值相同,则可使用此特性。可以将此特性应用于所有属性。
  • DefaultPropertyAttribute – 表示类的默认属性。在网格中选择某个类时,将首先突出显示该类的默认属性。可以将此特性应用于 AppSettings 类。
  现在,我们将其中的一些特性应用于 AppSettings 类,以更改属性在 PropertyGrid 中的显示方式。
None.gif // C#
None.gif
[DefaultPropertyAttribute( " SaveOnClose " )]
ExpandedBlockStart.gifContractedBlock.gif
public   class  AppSettings dot.gif {
InBlock.gif
private bool saveOnClose = true;
InBlock.gif
private string greetingText = "欢迎使用应用程序!";
InBlock.gif
private int maxRepeatRate = 10;
InBlock.gif
private int itemsInMRU = 4;
InBlock.gif
InBlock.gif
private bool settingsChanged = false;
InBlock.gif
private string appVersion = "1.0";
InBlock.gif
InBlock.gif[CategoryAttribute(
"文档设置"),
InBlock.gifDefaultValueAttribute(
true)]
InBlock.gif
public bool SaveOnClose
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gifreturn saveOnClose; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ saveOnClose = value;}
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif[CategoryAttribute(
"全局设置"),
InBlock.gifReadOnlyAttribute(
true),
InBlock.gifDefaultValueAttribute(
"欢迎使用应用程序!")]
InBlock.gif
public string GreetingText
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gifreturn greetingText; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ greetingText = value; }
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif[CategoryAttribute(
"全局设置"),
InBlock.gifDefaultValueAttribute(
4)]
InBlock.gif
public int ItemsInMRUList
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gifreturn itemsInMRU; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ itemsInMRU = value; }
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif[DescriptionAttribute(
"以毫秒表示的文本重复率。"),
InBlock.gifCategoryAttribute(
"全局设置"),
InBlock.gifDefaultValueAttribute(
10)]
InBlock.gif
public int MaxRepeatRate
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gifreturn maxRepeatRate; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ maxRepeatRate = value; }
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif[BrowsableAttribute(
false),
InBlock.gifDefaultValueAttribute(
false)]
InBlock.gif
public bool SettingsChanged
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gifreturn settingsChanged; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ settingsChanged = value; }
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif[CategoryAttribute(
"版本"),
InBlock.gifDefaultValueAttribute(
"1.0"),
InBlock.gifReadOnlyAttribute(
true)]
InBlock.gif
public string AppVersion
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gifreturn appVersion; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ appVersion = value; }
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

将这些特性应用于 AppSettings 类后,编译并运行该应用程序。下面的屏幕快照显示了应用程序的外观。

图 3:PropertyGrid 中显示的带有类别和默认值的属性

  使用此版本的选项窗口后,您会注意到以下几点:
  • 显示窗口时,将首先突出显示 SaveOnClose 属性。
  • 选中 MaxRepeatRate 属性时,说明帮助窗格中将显示“以毫秒表示的文本重复率”。
  • SaveOnClose 属性显示在“文档设置”类别下。其他属性分别显示在“全局设置”和“版本”类别下。
  • SettingsChanged 属性将不再显示。
  • AppVersion 属性为只读。只读属性以灰显文本显示。
  • 如果 SaveOnClose 属性包含的值不是 true,该值将以粗体显示。PropertyGrid 使用粗体文本表示包含非默认值的属性。

转载于:https://www.cnblogs.com/qyfan82/archive/2007/08/27/870901.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值