ListView.ListViewGroupCollection.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Windows.Data;
  4. namespace System.Windows.Forms
  5. {
  6. public class ListViewGroupCollection : CollectionBase, IEnumerable
  7. {
  8. private ListView owner { get; }
  9. private Dictionary<string, ListViewGroup> groups { get; }
  10. private CollectionView CollectionView => (CollectionView)CollectionViewSource.GetDefaultView(owner.Items);
  11. public ListViewGroup this[string key] => groups[key];
  12. public new int Count => groups.Count;
  13. private void AddGroupView()
  14. {
  15. var view = CollectionView;
  16. if (view.GroupDescriptions.Count == 0)
  17. view.GroupDescriptions.Add(new PropertyGroupDescription("Group"));
  18. }
  19. private void ClearGroupView()
  20. {
  21. if (groups.Count == 0)
  22. CollectionView.GroupDescriptions.Clear();
  23. }
  24. public new IEnumerator GetEnumerator() => groups.Values.GetEnumerator();
  25. public void Add(string key, string header)
  26. {
  27. AddGroupView();
  28. groups[key] = new ListViewGroup() { Header = header };
  29. }
  30. public new void RemoveAt(int index) { }
  31. public void Remove(string key)
  32. {
  33. groups.Remove(key);
  34. ClearGroupView();
  35. }
  36. public new void Clear()
  37. {
  38. groups.Clear();
  39. ClearGroupView();
  40. }
  41. internal ListViewGroupCollection(ListView owner)
  42. {
  43. this.owner = owner;
  44. groups = new();
  45. }
  46. }
  47. }