DynamicExportMappingGrid.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Windows.Media.Imaging;
  6. using InABox.Core;
  7. using InABox.WPF;
  8. namespace InABox.DynamicGrid;
  9. public class DynamicExportMappingGrid : DynamicGrid<ImportMapping>
  10. {
  11. private readonly BitmapImage selected = Wpf.Resources.Bullet_Tick.AsBitmapImage();
  12. public DynamicExportMappingGrid()
  13. {
  14. HideBlank = false;
  15. Items = new List<ImportMapping>();
  16. Selected = new List<ImportMapping>();
  17. ActionColumns.Add(new DynamicImageColumn(SelectedImage, SelectedAction));
  18. HiddenColumns.Add(x => x.Key);
  19. }
  20. public List<ImportMapping> Items { get; }
  21. public List<ImportMapping> Selected { get; }
  22. public bool HideBlank { get; set; }
  23. public override void DeleteItems(params CoreRow[] rows)
  24. {
  25. var deletes = new List<ImportMapping>();
  26. foreach (var row in rows)
  27. deletes.Add(Items[row.Index]);
  28. Items.RemoveAll(x => deletes.Contains(x));
  29. Selected.RemoveAll(x => deletes.Contains(x));
  30. }
  31. public override ImportMapping LoadItem(CoreRow row)
  32. {
  33. return Items[row.Index];
  34. }
  35. protected override void Reload(
  36. Filters<ImportMapping> criteria, Columns<ImportMapping> columns, ref SortOrder<ImportMapping>? sort,
  37. CancellationToken token, Action<CoreTable?, Exception?> action)
  38. {
  39. var result = new CoreTable();
  40. result.LoadColumns(typeof(ImportMapping));
  41. if (HideBlank)
  42. result.LoadRows(Items.Where(x => Selected.Contains(x)));
  43. else
  44. result.LoadRows(Items);
  45. action.Invoke(result, null);
  46. }
  47. public override void SaveItem(ImportMapping item)
  48. {
  49. if (!Items.Contains(item))
  50. Items.Add(item);
  51. }
  52. protected override DynamicGridColumns LoadColumns()
  53. {
  54. return new DynamicGridColumns
  55. {
  56. new() { ColumnName = "Property", Width = 0 }
  57. };
  58. }
  59. public void ClearAll()
  60. {
  61. Selected.Clear();
  62. Selected.AddRange(Items.Where(x => x.Key));
  63. Refresh(false, true);
  64. }
  65. public void SelectAll()
  66. {
  67. Selected.Clear();
  68. Selected.AddRange(Items);
  69. Refresh(false, true);
  70. }
  71. private BitmapImage SelectedImage(CoreRow? arg)
  72. {
  73. if (arg == null)
  74. return selected;
  75. var property = arg.Get<ImportMapping, string>(x => x.Property);
  76. return Selected.Any(x => x.Property.Equals(property)) ? selected : null;
  77. }
  78. private bool SelectedAction(CoreRow? arg)
  79. {
  80. var property = arg?.Get<ImportMapping, string>(x => x.Property);
  81. var items = arg == null ? Items.ToArray() : new[] { Items.Where(x => x.Property.Equals(property)).First() };
  82. foreach (var item in items)
  83. if (Selected.Contains(item) && !item.Key)
  84. Selected.Remove(item);
  85. else
  86. Selected.Add(item);
  87. return true;
  88. }
  89. }