DynamicExportMappingGrid.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. .AddFluent<ImportMapping>(x => x.Property, width: 0);
  56. }
  57. public void ClearAll()
  58. {
  59. Selected.Clear();
  60. Selected.AddRange(Items.Where(x => x.Key));
  61. Refresh(false, true);
  62. }
  63. public void SelectAll()
  64. {
  65. Selected.Clear();
  66. Selected.AddRange(Items);
  67. Refresh(false, true);
  68. }
  69. private BitmapImage SelectedImage(CoreRow? arg)
  70. {
  71. if (arg == null)
  72. return selected;
  73. var property = arg.Get<ImportMapping, string>(x => x.Property);
  74. return Selected.Any(x => x.Property.Equals(property)) ? selected : null;
  75. }
  76. private bool SelectedAction(CoreRow? arg)
  77. {
  78. var property = arg?.Get<ImportMapping, string>(x => x.Property);
  79. var items = arg == null ? Items.ToArray() : new[] { Items.Where(x => x.Property.Equals(property)).First() };
  80. foreach (var item in items)
  81. if (Selected.Contains(item) && !item.Key)
  82. Selected.Remove(item);
  83. else
  84. Selected.Add(item);
  85. return true;
  86. }
  87. }