有没有办法在运行时更改propertygrid griditem行为?我需要的是我有一个属性说“教育”,如果用户选择“大师”,那么下一个网格项目将显示与下拉控制中的大师相关的教育。如果用户在教育中选择“其他”,则下一个控件将是文本框类型以获取用户的输入。我知道如何显示下拉控件,只有我需要根据用户选择来切换这两个控件。
还有一个问题,有没有办法在运行时将griditem属性“可浏览”设置为false / true?
这可能吗?
答案 0 :(得分:1)
由于你的后样本代码,以下内容应该让你开始:
//Represents each property to show in the control, required since
//PropertyDescriptor is declared abstract
public class MyPropertyDescriptor : PropertyDescriptor
{
public MyPropertyDescriptor(string name, Attribute[] attrs) : base(name, attrs)
{
}
}
//This is the class that is bound to the PropertyGrid. Using
//CustomTypeDescriptor instead of ICustomTypeDescriptor saves you from
//having to implement all the methods in the interface which are stubbed out
//or default to the implementation in TypeDescriptor
public class MyClass : CustomTypeDescriptor
{
//This is the property that controls what other properties will be
//shown in the PropertyGrid, by attaching the RefreshProperties
//attribute, this will tell the PropertyGrid to query the bound
//object for the list of properties to show by calling your implementation
//of GetProperties
[RefreshProperties(RefreshProperties.All)]
public int ControllingProperty { get; set; }
//Dependent properties that can be dynamically added/removed
public int SomeProp { get; set; }
public int SomeOtherProp { get; set; }
//Return the list of properties to show
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
List<MyPropertyDescriptor> props = new List<MyPropertyDescriptor>();
//Insert logic here to determine what properties need adding to props
//based on the current property values
return PropertyDescriptorCollection(props.ToArray());
}
}
现在,当您将MyClass
的实例绑定到PropertyGrid时,将调用GetProperties
的实现,这使您有机会通过相应地填充属性集合来确定要显示的属性。