using System.Collections; using System.Collections.Generic; using System.Windows.Data; namespace System.Windows.Forms { public class ListViewGroupCollection : CollectionBase, IEnumerable { private ListView owner { get; } private Dictionary groups { get; } private CollectionView CollectionView => (CollectionView)CollectionViewSource.GetDefaultView(owner.Items); public ListViewGroup this[string key] => groups[key]; public new int Count => groups.Count; private void AddGroupView() { var view = CollectionView; if (view.GroupDescriptions.Count == 0) view.GroupDescriptions.Add(new PropertyGroupDescription("Group")); } private void ClearGroupView() { if (groups.Count == 0) CollectionView.GroupDescriptions.Clear(); } public new IEnumerator GetEnumerator() => groups.Values.GetEnumerator(); public void Add(string key, string header) { AddGroupView(); groups[key] = new ListViewGroup() { Header = header }; } public new void RemoveAt(int index) { } public void Remove(string key) { groups.Remove(key); ClearGroupView(); } public new void Clear() { groups.Clear(); ClearGroupView(); } internal ListViewGroupCollection(ListView owner) { this.owner = owner; groups = new(); } } }