C# 在 CategorizedAlphabetical 排序的 ProperyGrid 中选择第一行

标签 c# select propertygrid

我已将 ProperyGrid 加载为已分类的 PropertySpec 并设置为 CategorizedAlphabetical 排序。当表单运行类别时,类别中的项目将被排序。一个烦人的人工制品是 PropertyGrid 默认情况下选择列表排序后的第一项,有时它会 ScrollView 以选择。如果项目列表很长,您最终会看到列表滚动到中间的某个位置。

因为 PropertySpec 可以在运行时创建,所以我希望在加载表单时始终显示列表的顶部。 PropertyGrid 不会“轻易”公开集合,当然也不会按顺序公开。在谷歌搜索之后,我相信这是不可能的?

最佳答案

我想出了下面的代码来证明不是这样。

Snippet 将选择排序列表的第一个类别。人们也可以选择该类别中的第一项来扩展该方法,但对于我的需要来说,这是不必要的。

// 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].Select(); 而不是遍历并检查每个项目。如果您想使用该快捷方式,则必须断言列表不为空,但这会提高一些性能...

关于C# 在 CategorizedAlphabetical 排序的 ProperyGrid 中选择第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6556885/

相关文章:

MySQL逻辑操作SELECT查询

mysql - 如果标题 1 在 MySQL 中不存在,则选择标题 2

c# - 更改 PropertyGrid 左侧集合编辑器/ View 的宽度

c# - MarshalAsAttribute 字符串数组

c# - 从 c# 2.0 中的一系列值中获取不同值的高效逻辑

c# - 不正确的语法 ASP.NET

c# - MVC 单选按钮选择一个

sql - postgresql如何连接多个表以获得逗号分隔的输出

c# - PropertyGrid 在 UITypeEditor 或 TypeConverter 的帮助下编辑后执行操作以更改其他属性

c# - 在 propertygrid 中填充 JSON