如何在propertygrid c#中的属性中设置BrowseAttribute true或false?

时间:2012-08-22 16:14:49

标签: c# propertygrid

  

可能重复:
  Conditional “Browsable” Attribute

我定义了具有一些属性的AppSettings类。在我的表单中,当我click Button1时,我想要显示属性1和2(1,2显示,其他属性隐藏或不显示),当click Button2时,我想要显示属性2和3( 1是隐藏,2,3是显示,其他属性是隐藏或不显示),我该怎么办?

public class AppSettings
{
    [BrowsableAttribute(true), CategoryAttribute("Document Settings"), DefaultValueAttribute(true)]
    public bool SaveOnClose{ get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Global Settings"), ReadOnlyAttribute(true), DefaultValueAttribute("Welcome to AppDev!")]
    public string GreetingText { get; set; }

    [BrowsableAttribute(true), DescriptionAttribute("The rate in milliseconds that the text will repeat."), CategoryAttribute("Global Settings"), DefaultValueAttribute(10)]
    public int MaxRepeatRate { get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Global Settings"), DefaultValueAttribute(4)]
    public int ItemsInMRUList { get; set; }

    [BrowsableAttribute(true), DefaultValueAttribute(false)]
    public bool SettingsChanged { get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Version"), DefaultValueAttribute("1.0"), ReadOnlyAttribute(true)]
    public string AppVersion { get; set; }
}

我希望动态更改BrowseAttribute为true或false。我该怎么办?

表格代码是:

AppSettings AppSet = new AppSettings();

AppSet.AppVersion = "2.3";
AppSet.SaveOnClose = true;
AppSet.GreetingText = "Welcome to Dar!";
AppSet.ItemsInMRUList = 4;
AppSet.MaxRepeatRate = 10;
AppSet.SettingsChanged = false;

...

propertyGrid1.SelectedObject = AppSet;

此更改有错误:

public static bool state = true;
BrowsableAttribute(state)

错误:

  

属性参数必须是常量表达式,typeof表达式   或属性参数类型

的数组创建表达式

1 个答案:

答案 0 :(得分:6)

对于过滤,我只会更改BrowsableAttributes的{​​{1}}。在下面,我:

  • 定义自定义属性PropertyGrid,该属性描述何时应该可见的内容
    • 覆盖[DisplayMode(...)]以指示何时应将属性视为等效
  • 使用属性
  • 修饰您的类型IsMatch上的部分设置
  • 更改网格上的AppSettings,指定特定的BrowsableAttributes,然后显示
  • 使用其他设置重复

以下是代码:

DisplayModeAttribute
相关问题