首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在运行时更改属性网格网格项行为?

如何在运行时更改属性网格网格项行为?
EN

Stack Overflow用户
提问于 2011-09-16 18:05:00
回答 1查看 1.1K关注 0票数 0
  1. 是否有方法在运行时更改属性网格网格项行为?我需要的是,我有一个属性,说“教育”,如果用户选择“大师”,那么下一个网格项目将显示教育硕士在下拉控制。如果用户在“教育”中选择“其他人”,那么下一个控件将是textbox类型,以接受用户的输入。我知道如何显示下拉控件,只是我需要根据用户选择来切换这两个控件。
  2. 还有一个问题,有没有办法在运行时将网格项属性“可浏览”设置为false/true?

这个是可能的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-17 21:12:03

由于您的后续示例代码,下面这样的内容应该可以帮助您开始工作:

代码语言:javascript
运行
复制
//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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7448658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档