可能重复:
有条件的“可浏览”属性
我定义AppSettings
了具有一些属性的类。在我的表单中,当 I 时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
为真或假。我该怎么做 ?
表格代码是:
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 表达式或数组创建表达式