热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#学习教程:在show上展开C#propertygrid分享

在show上展开C#propertygrid我对房产网格有疑问。当表单显示时,我想要一个组进行扩展而不是折叠。我在网上搜索了很多,但还没找到。有什么想法吗。如果要扩展网格中的所有项

在show上展开C#propertygrid

我对房产网格有疑问。 当表单显示时,我想要一个组进行扩展而不是折叠。 我在网上搜索了很多,但还没找到。 有什么想法吗。

如果要扩展网格中的所有项目,这非常简单。 属性网格有一个方法:

propertyGrid.ExpandAllGridItems(); 

如果它是您要扩展的特定组,则可以使用此方法:

 private static void ExpandGroup(PropertyGrid propertyGrid, string groupName) { GridItem root = propertyGrid.SelectedGridItem; //Get the parent while (root.Parent != null) root = root.Parent; if (root != null) { foreach (GridItem g in root.GridItems) { if (g.GridItemType == GridItemType.Category && g.Label == groupName) { g.Expanded = true; break; } } } } 

就个人而言,我接受了Simon的回答,并用它创建了一个扩展,并添加了使用Attributes声明扩展对象的Aspect Oriented Programming技术(如果你愿意,你可以添加你的味道,这很容易):

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace HQ.Util.WinFormUtil { public static class PropertyGridExtension { // ****************************************************************** public static void ExpandGroupName(this PropertyGrid propertyGrid, string groupName) { foreach (GridItem gridItem in propertyGrid.SelectedGridItem.GridItems) { if (gridItem != null) { if (gridItem.GridItemType == GridItemType.Category && gridItem.Label == groupName) { gridItem.Expanded = true; } } } } // ****************************************************************** public static void ExpandItemWithInitialExpandedAttribute(this PropertyGrid propertyGrid) { ExpandItemWithInitialExpandedAttribute(propertyGrid, propertyGrid.SelectedGridItem); } // ****************************************************************** private static void ExpandItemWithInitialExpandedAttribute(PropertyGrid propertyGrid, GridItem gridItem) { if (gridItem != null) { if (gridItem.GridItemType == GridItemType.Property && gridItem.Expandable) { object[] objs = gridItem.Value.GetType().GetCustomAttributes(typeof(PropertyGridInitialExpandedAttribute), false); if (objs.Length > 0) { if (((PropertyGridInitialExpandedAttribute) objs[0]).InitialExpanded) { gridItem.Expanded = true; } } } foreach (GridItem childItem in gridItem.GridItems) { ExpandItemWithInitialExpandedAttribute(propertyGrid, childItem); } } } // ****************************************************************** } } 

而这堂课

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HQ.Util.WinFormUtil { public class PropertyGridInitialExpandedAttribute : Attribute { public bool InitialExpanded { get; set; } public PropertyGridInitialExpandedAttribute(bool initialExpanded) { InitialExpanded = initialExpanded; } } } 

用法是:

 [PropertyGridInitialExpanded(true)] public class YourClass { ... } 

电话是:

 this.propertyGrid.ExpandItemWithInitialExpandedAttribute(); 

快乐的编码;-)

(重新混合Simon上面的答案和Eric的答案……)

要扩展SelectedGridItem的所有兄弟节点:

 public static void ExpandItemWithInitialExpandedAttribute(this PropertyGrid propertyGrid) { foreach (GridItem item in propertyGrid.SelectedGridItem.Parent.GridItems) { ExpandItemWithInitialExpandedAttribute(propertyGrid, item); } } 

这些枚举器扩展允许我做我想做的一切。

上述就是C#学习教程:在show上展开C#propertygrid分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—编程笔记

 public static class PropertyGridExtensions { public static IEnumerable EnumerateGroups(this PropertyGrid propertyGrid) { if (propertyGrid.SelectedGridItem == null) yield break; foreach (var i in propertyGrid.EnumerateItems()) { if (i.Expandable) { yield return i; } } } public static IEnumerable EnumerateItems(this PropertyGrid propertyGrid) { if (propertyGrid.SelectedGridItem == null) yield break; var root = propertyGrid.SelectedGridItem; while (root.Parent != null) root = root.Parent; yield return root; foreach (var i in root.EnumerateItems()) { yield return i; } } public static GridItem GetGroup(this PropertyGrid propertyGrid, string label) { if (propertyGrid.SelectedGridItem == null) return null; foreach (var i in propertyGrid.EnumerateItems()) { if (i.Expandable && i.Label == label) { return i; } } return null; } private static IEnumerable EnumerateItems(this GridItem item) { foreach (GridItem i in item.GridItems) { yield return i; foreach (var j in i.EnumerateItems()) { yield return j; } } } } 


推荐阅读
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
author-avatar
风冷泻千山
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有