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

发布于 2024-11-18 07:45:31 字数 293 浏览 1 评论 0原文

我已经加载了分类 PropertySpec 的 ProperyGrid 并设置为 CategorizedAlphabetical 排序。当表单运行类别时,类别内的项目将被排序。一个令人讨厌的现象是,PropertyGrid 默认情况下会在列表排序后选择第一个项目,有时它会将视图滚动到所选内容。如果项目列表很长,您最终会看到列表滚动到中间的某个位置。

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

I have ProperyGrid loaded with categorised PropertySpec and set to CategorizedAlphabetical sort. When form runs categories then items within categories are sorted. An annoying artefact is that PropertyGrid by default selects the first item after list was sorted and sometimes it scrolls view to selection. If item list is long you end up seeing list scrolled to somewhere in the middle.

Since PropertySpec can be created at runtime I want to always show the top of list on form load. PropertyGrid does not 'easily' expose collections and certainly not in ordered sequence. After googling around I am lead to believe this is not possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枯叶蝶 2024-11-25 07:45:31

我想出了下面的代码证明事实并非如此。

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

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

I came up with below code which proves otherwise.

Snippet will select fist category of sorted list. One could also select first item in that category expanding on the method but for my needs that was unnecessary.

// 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;
    }
}

Hope this will help others as well.

The simplified method of actually selecting category once you have sorted list would be to sortedCats[0].Select(); instead of looping through and checking each item. You would have to assert the list is not empty if you wanted to use that shortcut but that would gives some performance improvement...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文