【问题标题】:Sort ListViewGroups of a ListView alphabetically按字母顺序对 ListView 的 ListViewGroups 进行排序
【发布时间】:2012-09-14 20:19:05
【问题描述】:

是否可以在运行时按字母顺序对 Windows 窗体 ListView 的所有 ListViewGroups 进行排序?

或者我在添加组时是否必须手动实现排序(使用 ListViewGroupCollection 的“Insert”方法)?如果是这种情况,有人可以告诉我如何做到这一点吗?

【问题讨论】:

    标签: .net winforms listview sorting alphabetical


    【解决方案1】:

    在 WinForms ListView 中,您必须手动进行排序:

    ListViewGroup[] groups = new ListViewGroup[this.listView1.Groups.Count];
    
    this.listView1.Groups.CopyTo(groups, 0);
    
    Array.Sort(groups, new GroupComparer());
    
    this.listView1.BeginUpdate();
    this.listView1.Groups.Clear();
    this.listView1.Groups.AddRange(groups);
    this.listView1.EndUpdate();
    
    ...
    
    class GroupComparer : IComparer
    {
        public int Compare(object objA, object objB)
        {
            return ((ListViewGroup)objA).Header.CompareTo(((ListViewGroup)objB).Header);
        }
    }
    

    .NET ListView 中的组非常讨厌 - 它们的外观和行为就像旧 Win32 ListView 和 Windows 资源管理器之间的混合...

    所以我会推荐你​​Better ListView component,它支持开箱即用的分组排序:

    this.betterListView1.Groups.Sort(new GroupComparer());
    
    ...
    
    class GroupComparer : IComparer<BetterListViewGroup>
    {
        public int Compare(BetterListViewGroup groupA, BetterListViewGroup groupB)
        {
            return groupA.Header.CompareTo(groupB.Header);
        }
    }
    

    此外,这些组的外观和行为就像在 Windows 资源管理器中一样,是可折叠的,即使对它们进行排序也不会发生闪烁。

    【讨论】:

    • 谢谢!我的 ListView 组件中缺少组排序,我已经实现了可折叠组和双缓冲 / uxstyles。
    猜你喜欢
    • 1970-01-01
    • 2013-11-09
    • 2015-04-04
    • 2011-08-29
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多