由於您的示例代碼之後,像下面應該讓你開始:
//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
執行將被調用,讓你有機會通過相應地填充屬性集合來確定要顯示的屬性。
這兩個問題的答案都與[本文] [1]中的相同。 [1]:http://stackoverflow.com/questions/7359649/c-propertygrid-how-to-change-visible-properties-at-runtime/7373746#7373746 – DeCaf
我尋找示例代碼。 –