关于PropertyGrid控件的排序问题
前些天,由于在项目中需要用到PropertyGrid这个控件,展现其所在控件的某些属性,由于有些控件的属性较多,不易浏览,而且PropertyGrid的排序默认的按照字母的顺序排列的,这样导致在在某些属性想要排在第一位非常不方便,于是我总结了网友们的一些思路,自己便解决呢!现在来说说解决思路:
1.首先为PropertyGrid添加SelectedObjectsChanged事件!
private void propertyGrid_flow_SelectedObjectsChanged(object sender, EventArgs e)
{
propertyGrid_flow.Tag = propertyGrid_flow.PropertySort;
propertyGrid_flow.PropertySort = PropertySort.CategorizedAlphabetical;
propertyGrid_flow.Paint += new PaintEventHandler(propertyGrid_flow_Paint);
}
2.为PropertyGrid添加Paint事件! 这其中就是最核心的代码,就是按照propertyGrid默认属性排序!
var categorysinfo = propertyGrid_flow.SelectedObject.GetType().GetField("categorys", BindingFlags.NonPublic | BindingFlags.Instance);
if (categorysinfo != null)
{
var categorys = categorysinfo.GetValue(propertyGrid_flow.SelectedObject) as List<String>;
propertyGrid_flow.CollapseAllGridItems();
GridItemCollection currentPropEntries = propertyGrid_flow.GetType().GetField("currentPropEntries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(propertyGrid_flow) as GridItemCollection;
var newarray = currentPropEntries.Cast<GridItem>().OrderBy((t) => categorys.IndexOf(t.Label)).ToArray();
currentPropEntries.GetType().GetField("entries", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(currentPropEntries, newarray);
propertyGrid_flow.ExpandAllGridItems();
propertyGrid_flow.PropertySort = (PropertySort)propertyGrid_flow.Tag;
}
propertyGrid_flow.Paint -= new PaintEventHandler(propertyGrid_flow_Paint);
3.在其中可以看到有个“categorys”变量,其中是是在propertyGrid的属性中命名:
[TypeConverter(typeof(PropertySorter))]
public class UctlNodeStepProperty : PropertyGird
{
private List<string> categorys = new List<string>(){ "A", "B", "C", "D" };
}
4. 罗列propertyGrid的属性:
private string _a1="";
private string _a2="";
private string _a3="";
private string _b1="";
private string _c1="";
private string _d1=""; [Browsable(true), Category("A"), ShowChinese("描述"),PropertyOrder()]
public string A1
{
get { return _a1; }
set { _a1 = value; }
}
[Browsable(true), Category("A"), ShowChinese("描述"),PropertyOrder()]
public string A2
{
get { return _a2; }
set { _a2 = value; }
}
[Browsable(true), Category("A"), ShowChinese("描述"),PropertyOrder()]
public string A3
{
get { return _a3; }
set { _a3 = value; }
}
[Browsable(true), Category("B"), ShowChinese("描述")]
public string B1
{
get { return _b1; }
set { _b1 = value; }
}
[Browsable(true), Category("C"), ShowChinese("描述")]
public string C1
{
get { return _c1; }
set { _c1 = value; }
}
[Browsable(true), Category("D"), ShowChinese("描述")]
public string D1
{
get { return _d1; }
set { _d1 = value; }
}
5.我想大家也看到了其中的代码,其中有个属性“PropertyOrder”,下面就是PropertyOtder类的代码:
public class PropertySorter : ExpandableObjectConverter
{
#region Methods
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
} public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
//
// This override returns a list of properties in order
//
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes);
ArrayList orderedProperties = new ArrayList();
foreach (PropertyDescriptor pd in pdc)
{
Attribute attribute = pd.Attributes[typeof(PropertyOrderAttribute)];
if (attribute != null)
{
//
// If the attribute is found, then create an pair object to hold it
//
PropertyOrderAttribute poa = (PropertyOrderAttribute)attribute;
orderedProperties.Add(new PropertyOrderPair(pd.Name,poa.Order));
}
else
{
//
// If no order attribute is specifed then given it an order of 0
//
orderedProperties.Add(new PropertyOrderPair(pd.Name,));
}
}
//
// Perform the actual order using the value PropertyOrderPair classes
// implementation of IComparable to sort
//
orderedProperties.Sort(); //
// Build a string list of the ordered names
//
ArrayList propertyNames = new ArrayList();
foreach (PropertyOrderPair pop in orderedProperties)
{
propertyNames.Add(pop.Name);
}
//
// Pass in the ordered list for the PropertyDescriptorCollection to sort by
//
return pdc.Sort((string[])propertyNames.ToArray(typeof(string)));
}
#endregion
} #region Helper Class - PropertyOrderAttribute
[AttributeUsage(AttributeTargets.Property)]
public class PropertyOrderAttribute : Attribute
{
//
// Simple attribute to allow the order of a property to be specified
//
private int _order;
public PropertyOrderAttribute(int order)
{
_order = order;
} public int Order
{
get
{
return _order;
}
}
}
#endregion #region Helper Class - PropertyOrderPair
public class PropertyOrderPair : IComparable
{
private int _order;
private string _name;
public string Name
{
get
{
return _name;
}
} public PropertyOrderPair(string name, int order)
{
_order = order;
_name = name;
} public int CompareTo(object obj)
{
//
// Sort the pair objects by ordering by order value
// Equal values get the same rank
//
int otherOrder = ((PropertyOrderPair)obj)._order;
if (otherOrder == _order)
{
//
// If order not specified, sort by name
//
string otherName = ((PropertyOrderPair)obj)._name;
return string.Compare(_name,otherName);
}
else if (otherOrder > _order)
{
return -;
}
return ;
}
}
#endregion
6.如果想隐藏Font这样的属性的一些英文属性,仅保留中文属性的话:(因为按照上面的步骤,你会发现,像Font这样的属性会同时包含中文和因为的属性,发现英文的会有点多余)
public class HideFontSubPropConverter : FontConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return new PropertyDescriptorCollection(null);
}
}
/// <summary>
/// string不展开
/// </summary>
public class HideStringSubPropConverter : StringConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return new PropertyDescriptorCollection(null);
}
}
/// <summary>
/// size不展开
/// </summary>
public class HideSizeSubPropConverter : SizeConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return new PropertyDescriptorCollection(null); ;
}
}
关于PropertyGrid控件的排序问题的更多相关文章
- PropertyGrid控件由浅入深(二):基础用法
目录 PropertyGrid控件由浅入深(一):文章大纲 PropertyGrid控件由浅入深(二):基础用法 控件的外观构成 控件的外观构成如下图所示: PropertyGrid控件包含以下几个要 ...
- PropertyGrid控件由浅入深(一):文章大纲
Winform中PropertyGrid控件是一个非常好用的对象属性编辑工具,对于Key-Value形式的数据的处理也是非常的好用. 因为Property控件设计良好,在很小的空间内可以展示很多的内容 ...
- WinForm小白的WPF初试一:从PropertyGrid控件,输出内容到Word(上)
学WinForm也就半年,然后转到WPF,还在熟悉中.最近拿到一个任务:从PropertyGrid控件,输出内容到Word.难点有: 一.PropertyGrid控件是WinForm控件,在WPF中并 ...
- C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值
关于PropertyGrid控件的详细用法请参考文献: 1.C# PropertyGrid控件应用心得 2.C#自定义PropertyGrid属性 首先定义一个要在下拉框显示的控件: using Sy ...
- WinForm窗体PropertyGrid控件的使用
使用过 Microsoft Visual Basic 或 Microsoft Visual Studio .NET的朋友,一定使用过属性浏览器来浏览.查看或编辑一个或多个对象的属性..NET 框架 P ...
- C# PropertyGrid控件应用心得
何处使用 PropertyGrid 控件 在应用程序中的很多地方,您都可以使用户与 PropertyGrid 进行交互,从而获得更丰富的编辑体验.例如,某个应用程序包含多个用户可以设置的“设置”或选项 ...
- propertyGrid控件 z
1.如果属性是enum类型,那么自然就是下拉的. 2.如果是你自定义的下拉数据,那么需要用到转换属性标签TypeConverter 参见: http://blog.csdn.net/luyifeini ...
- 自定义PropertyGrid控件【转】
自定义PropertyGrid控件 这篇随笔其实是从别人博客上载录的.感觉很有价值,整理了一下放在了我自己的博客上,希望原作者不要介意. 可自定义PropertyGrid控件的属性.也可将属性名称显示 ...
- C# PropertyGrid控件应用心得 【转】
源文 : http://blog.csdn.net/luyifeiniu/article/details/5426960 c#stringattributesobjectmicrosoftclass ...
随机推荐
- js判断css动画效果是否结束
<!-- css样式 --> <style> .test{ width: 100px; height: 100px; transition: all 5s; backgroun ...
- DB水平切换要点
分区健选择 数据应该怎样拆分,依照什么纬度来拆分 节点路由 应用程序写死/客户端(TDDL,cobar-client)/中间层(cobar-server) 固定分配/动态分配/混合 分片数据均衡 某些 ...
- 算法之去掉vetor集合中的重复元素
public class DropRepetition { public static void main(String[] args) { Vector<String> v = new ...
- poj1325(Machine Schedule)
题目链接:传送门 题目大意:有k个任务,可以在 A 机器的 x 位上完成,也可以在 B 机器的 y 位上完成.问最少需要多少个点位即可完成所有任务. 题目思路:求最小点覆盖. 把 A 机器,B 机器看 ...
- ios中沙盒
打开模拟器沙盒目录 下面看看模拟器的沙盒文件夹在mac电脑上的什么位置. 文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,他的目录其实是Library. 2.1 方法1.可以设置显示隐藏文 ...
- [LintCode] 二叉树的后序遍历
The recursive solution is trivial and I omit it here. Iterative Solution using Stack (O(n) time and ...
- 深入理解Mysql索与事务隔离级别
1. 概述 1.1 定义 锁是计算机协调多个进程或线程并发访问某一资源的机制. 在数据库中,除了传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供需要用户共享的资源.如何保证数据并 ...
- 《深入理解Linux网络技术内幕》阅读笔记 --- 路由表
路由表基本概念 1.路由是由多个不同的数据结构的组合来描述的,每个数据结构代表路由信息的不同部分.例如,一个fib_node对应一个单独的子网,一个fib_alias对应一条路由.这样做的原因是只需通 ...
- ovs ovn 学习资料
0.A Primer on OVN http://blog.spinhirne.com/2016/09/a-primer-on-ovn.html 1.Open Virtual Networking W ...
- session补充