DynamicSelectorGrid.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using InABox.Core;
  2. using InABox.WPF;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Controls;
  9. using System.Windows.Media.Imaging;
  10. namespace InABox.DynamicGrid;
  11. public class DynamicSelectorGrid<T> : DynamicDataGrid<T>
  12. where T : Entity, IRemotable, IPersistent, new()
  13. {
  14. private static BitmapImage tick = InABox.Wpf.Resources.tick.AsBitmapImage();
  15. public HashSet<Guid> SelectedIDs { get; set; } = new();
  16. private HashSet<Guid> LastVisible = new();
  17. public delegate void SelectionChangedEvent(HashSet<Guid> selected);
  18. public event SelectionChangedEvent? SelectionChanged;
  19. public DynamicSelectorGrid(DynamicActionColumnPosition tickPosition)
  20. {
  21. ActionColumns.Add(new DynamicImageColumn(Selected_Image, Selected_Click)
  22. {
  23. Position = tickPosition
  24. });
  25. }
  26. private BitmapImage? Selected_Image(CoreRow? row)
  27. {
  28. if (row is null) return tick;
  29. return SelectedIDs.Contains(row.Get<T, Guid>(x => x.ID)) ? tick : null;
  30. }
  31. protected virtual Guid[] GetSelectedGuids(Guid id)
  32. {
  33. return new Guid[] { id };
  34. }
  35. protected IEnumerable<Guid> FilteredGuids
  36. {
  37. get
  38. {
  39. var predicates = GetFilterPredicates();
  40. return Data.Rows.Where(r =>
  41. {
  42. return predicates.All(x => x.Item2(r));
  43. }).Select(x => x.Get<T, Guid>(x => x.ID));
  44. }
  45. }
  46. protected override void DoFilterChanged()
  47. {
  48. base.DoFilterChanged();
  49. DoSelectionChanged();
  50. }
  51. private void DoSelectionChanged()
  52. {
  53. var ids = SelectedIDs.Intersect(FilteredGuids).ToHashSet();
  54. if(ids.Count != LastVisible.Count || ids.Any(x => !LastVisible.Contains(x)))
  55. {
  56. LastVisible = ids;
  57. SelectionChanged?.Invoke(LastVisible);
  58. }
  59. }
  60. private bool Selected_Click(CoreRow? row)
  61. {
  62. if (row is null)
  63. {
  64. var menu = new ContextMenu();
  65. menu.AddItem("Select All", null, SelectAll_Click);
  66. menu.AddItem("Deselect All", null, DeselectAll_Click);
  67. menu.IsOpen = true;
  68. return false;
  69. }
  70. var parent = row.Get<T, Guid>(x => x.ID);
  71. var ids = GetSelectedGuids(parent);
  72. if (SelectedIDs.Contains(parent))
  73. {
  74. foreach (var id in ids.Where(x => SelectedIDs.Contains(x)))
  75. {
  76. SelectedIDs.Remove(id);
  77. var update = Data.Rows.FirstOrDefault(r => r.Get<T, Guid>(x => x.ID) == id);
  78. if (update != null)
  79. InvalidateRow(update);
  80. }
  81. }
  82. else
  83. {
  84. foreach (var id in ids.Where(x => !SelectedIDs.Contains(x)))
  85. {
  86. SelectedIDs.Add(id);
  87. var update = Data.Rows.FirstOrDefault(r => r.Get<T, Guid>(x => x.ID) == id);
  88. if (update != null)
  89. InvalidateRow(update);
  90. }
  91. }
  92. DoSelectionChanged();
  93. InvalidateRow(row);
  94. return false;
  95. }
  96. private void DeselectAll_Click()
  97. {
  98. SelectedIDs.Clear();
  99. DoSelectionChanged();
  100. InvalidateGrid();
  101. }
  102. private void SelectAll_Click()
  103. {
  104. SelectedIDs = Data.Rows.Select(x => x.Get<T, Guid>(x => x.ID)).ToHashSet();
  105. DoSelectionChanged();
  106. InvalidateGrid();
  107. }
  108. protected override void DoReconfigure(DynamicGridOptions options)
  109. {
  110. base.DoReconfigure(options);
  111. options.Clear();
  112. }
  113. }