C# C选择分类法排序属性网格中的第一行
我已将PropertyGrid与Categorized PropertySpec一起加载,并设置为CategorizeDalphabetial排序。当窗体运行类别时,类别中的项目将被排序。一个恼人的事实是,PropertyGrid默认情况下会在列表排序后选择第一个项目,有时它会将视图滚动到所选内容。如果项目列表长,你会看到列表滚动到中间的某个地方。C# C选择分类法排序属性网格中的第一行,c#,select,propertygrid,C#,Select,Propertygrid,我已将PropertyGrid与Categorized PropertySpec一起加载,并设置为CategorizeDalphabetial排序。当窗体运行类别时,类别中的项目将被排序。一个恼人的事实是,PropertyGrid默认情况下会在列表排序后选择第一个项目,有时它会将视图滚动到所选内容。如果项目列表长,你会看到列表滚动到中间的某个地方。 由于PropertySpec可以在运行时创建,所以我希望在表单加载时始终显示列表的顶部。PropertyGrid不会“轻松”公开集合,当然也不会按顺
由于PropertySpec可以在运行时创建,所以我希望在表单加载时始终显示列表的顶部。PropertyGrid不会“轻松”公开集合,当然也不会按顺序公开。在谷歌搜索之后,我相信这是不可能的?我找到了下面的代码,证明了这一点 代码段将选择排序列表的第一个类别。人们也可以选择该类别中的第一个项目,扩展方法,但我的需要,这是不必要的
// bind the PropertyTable to PropertyGrid
this.pg_Prefs.SelectedObject = proptable;
// get selected item
GridItem gi = this.pg_Prefs.SelectedGridItem;
// get category for selected item
GridItem pgi = gi.Parent.Parent;
//sort categories
List<GridItem> sortedCats = new List<GridItem>(pgi.GridItems.Cast<GridItem>());
sortedCats.Sort(delegate(GridItem gi1, GridItem gi2) { return gi1.Label.CompareTo(gi2.Label); });
// loop to first category
for (int i = 0; i < pgi.GridItems.Count; i++)
{
if (pgi.GridItems[i] == gi) break; // in case full circle done
// select if first category
if (pgi.GridItems[i].Label == sortedCats[0].Label)
{
pgi.GridItems[i].Select();
break;
}
}
希望这也能帮助其他人
一旦您对列表进行了排序,实际选择类别的简化方法将是sortedCats[0]。选择;而不是循环检查每个项目。如果您想使用该快捷方式,则必须声明列表不是空的,但这将提高性能…我提出了下面的代码,证明了不是空的 代码段将选择排序列表的第一个类别。人们也可以选择该类别中的第一个项目,扩展方法,但我的需要,这是不必要的
// bind the PropertyTable to PropertyGrid
this.pg_Prefs.SelectedObject = proptable;
// get selected item
GridItem gi = this.pg_Prefs.SelectedGridItem;
// get category for selected item
GridItem pgi = gi.Parent.Parent;
//sort categories
List<GridItem> sortedCats = new List<GridItem>(pgi.GridItems.Cast<GridItem>());
sortedCats.Sort(delegate(GridItem gi1, GridItem gi2) { return gi1.Label.CompareTo(gi2.Label); });
// loop to first category
for (int i = 0; i < pgi.GridItems.Count; i++)
{
if (pgi.GridItems[i] == gi) break; // in case full circle done
// select if first category
if (pgi.GridItems[i].Label == sortedCats[0].Label)
{
pgi.GridItems[i].Select();
break;
}
}
希望这也能帮助其他人
一旦您对列表进行了排序,实际选择类别的简化方法将是sortedCats[0]。选择;而不是循环检查每个项目。如果您想使用该快捷方式,则必须声明列表不是空的,但这会提高性能