DynamicSelectorGrid.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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> VisibleGuids => GetVisibleRows().Select(r => r.Get<T, Guid>(x => x.ID));
  35. protected override void DoFilterChanged()
  36. {
  37. base.DoFilterChanged();
  38. DoSelectionChanged();
  39. }
  40. private void DoSelectionChanged()
  41. {
  42. var ids = SelectedIDs.Intersect(VisibleGuids).ToHashSet();
  43. SelectionChanged?.Invoke(ids);
  44. }
  45. private bool Selected_Click(CoreRow? row)
  46. {
  47. if (row is null)
  48. {
  49. var menu = new ContextMenu();
  50. menu.AddItem("Select All", null, SelectAll_Click);
  51. menu.AddItem("Deselect All", null, DeselectAll_Click);
  52. menu.IsOpen = true;
  53. return false;
  54. }
  55. var parent = row.Get<T, Guid>(x => x.ID);
  56. var ids = GetSelectedGuids(parent);
  57. if (SelectedIDs.Contains(parent))
  58. {
  59. foreach (var id in ids.Where(x => SelectedIDs.Contains(x)))
  60. {
  61. SelectedIDs.Remove(id);
  62. var update = Data.Rows.FirstOrDefault(r => r.Get<T, Guid>(x => x.ID) == id);
  63. if (update != null)
  64. InvalidateRow(update);
  65. }
  66. }
  67. else
  68. {
  69. foreach (var id in ids.Where(x => !SelectedIDs.Contains(x)))
  70. {
  71. SelectedIDs.Add(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. DoSelectionChanged();
  78. InvalidateRow(row);
  79. return false;
  80. }
  81. private void DeselectAll_Click()
  82. {
  83. SelectedIDs.Clear();
  84. DoSelectionChanged();
  85. InvalidateGrid();
  86. }
  87. private void SelectAll_Click()
  88. {
  89. SelectedIDs = Data.Rows.Select(x => x.Get<T, Guid>(x => x.ID)).ToHashSet();
  90. DoSelectionChanged();
  91. InvalidateGrid();
  92. }
  93. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  94. {
  95. base.DoReconfigure(options);
  96. options.Clear();
  97. }
  98. }