DynamicSelectorGrid.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. public delegate void SelectionChangedEvent(HashSet<Guid> selected);
  17. public event SelectionChangedEvent? SelectionChanged;
  18. public DynamicSelectorGrid(DynamicActionColumnPosition tickPosition)
  19. {
  20. ActionColumns.Add(new DynamicImageColumn(Selected_Image, Selected_Click)
  21. {
  22. Position = tickPosition
  23. });
  24. }
  25. private BitmapImage? Selected_Image(CoreRow? row)
  26. {
  27. if (row is null) return tick;
  28. return SelectedIDs.Contains(row.Get<T, Guid>(x => x.ID)) ? tick : null;
  29. }
  30. protected virtual Guid[] GetSelectedGuids(Guid id)
  31. {
  32. return new Guid[] { id };
  33. }
  34. protected IEnumerable<Guid> FilteredGuids
  35. {
  36. get
  37. {
  38. var predicates = GetFilterPredicates();
  39. return Data.Rows.Where(r =>
  40. {
  41. return predicates.All(x => x.Item2(r));
  42. }).Select(x => x.Get<T, Guid>(x => x.ID));
  43. }
  44. }
  45. protected override void DoFilterChanged()
  46. {
  47. base.DoFilterChanged();
  48. DoSelectionChanged();
  49. }
  50. private void DoSelectionChanged()
  51. {
  52. var ids = SelectedIDs.Intersect(FilteredGuids).ToHashSet();
  53. SelectionChanged?.Invoke(ids);
  54. }
  55. private bool Selected_Click(CoreRow? row)
  56. {
  57. if (row is null)
  58. {
  59. var menu = new ContextMenu();
  60. menu.AddItem("Select All", null, SelectAll_Click);
  61. menu.AddItem("Deselect All", null, DeselectAll_Click);
  62. menu.IsOpen = true;
  63. return false;
  64. }
  65. var parent = row.Get<T, Guid>(x => x.ID);
  66. var ids = GetSelectedGuids(parent);
  67. if (SelectedIDs.Contains(parent))
  68. {
  69. foreach (var id in ids.Where(x => SelectedIDs.Contains(x)))
  70. {
  71. SelectedIDs.Remove(id);
  72. var update = Data.Rows.FirstOrDefault(r => r.Get<T, Guid>(x => x.ID) == id);
  73. if (update != null)
  74. InvalidateRow(update);
  75. }
  76. }
  77. else
  78. {
  79. foreach (var id in ids.Where(x => !SelectedIDs.Contains(x)))
  80. {
  81. SelectedIDs.Add(id);
  82. var update = Data.Rows.FirstOrDefault(r => r.Get<T, Guid>(x => x.ID) == id);
  83. if (update != null)
  84. InvalidateRow(update);
  85. }
  86. }
  87. DoSelectionChanged();
  88. InvalidateRow(row);
  89. return false;
  90. }
  91. private void DeselectAll_Click()
  92. {
  93. SelectedIDs.Clear();
  94. DoSelectionChanged();
  95. InvalidateGrid();
  96. }
  97. private void SelectAll_Click()
  98. {
  99. SelectedIDs = Data.Rows.Select(x => x.Get<T, Guid>(x => x.ID)).ToHashSet();
  100. DoSelectionChanged();
  101. InvalidateGrid();
  102. }
  103. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  104. {
  105. base.DoReconfigure(options);
  106. options.Clear();
  107. }
  108. }