开发者

How to change propertygrid griditem behaviour in runtime?

开发者 https://www.devze.com 2023-04-05 15:40 出处:网络
Is there any way to change propertygrid griditem behavior in runtime? What I need is I have one property say \"Education\", if user selects \"Masters\" then the next griditem will show education rela
  1. Is there any way to change propertygrid griditem behavior in runtime? What I need is I have one property say "Education", if user selects "Masters" then the next griditem will show education related to master in dropdown control. If user selects "Others" in Education then next control will be textbox type to take user's input. I know how to sho开发者_运维技巧w dropdown control, only I need how I can toggle these two control based on user selection.

  2. One more question, is there any way to set griditem property "Browsable" to false/true in runtime?

Is this possible?


Since your after sample code, something like the following should get you started:

//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());
   }
}

Now when you bind an instance of MyClass to the PropertyGrid, your implementation of GetProperties will be called which gives you the opportunity to determine what properties you want to show by populating the property collection accordingly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号