【问题标题】:C# Selecting first row in CategorizedAlphabetical sorted ProperyGridC# 在 CategorizedAlphabetical 排序的 ProperyGrid 中选择第一行
【发布时间】:2011-07-02 12:06:17
【问题描述】:

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

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

【问题讨论】:

    标签: c# select propertygrid


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 2018-04-23
      • 1970-01-01
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多