DynamicExportMappingGrid.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Media.Imaging;
  5. using InABox.Core;
  6. using InABox.WPF;
  7. namespace InABox.DynamicGrid
  8. {
  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. protected override void Init()
  21. {
  22. }
  23. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  24. {
  25. //Options.AddRange(DynamicGridOption.RecordCount);
  26. }
  27. public List<ImportMapping> Items { get; }
  28. public List<ImportMapping> Selected { get; }
  29. public bool HideBlank { get; set; }
  30. protected override void DeleteItems(params CoreRow[] rows)
  31. {
  32. var deletes = new List<ImportMapping>();
  33. foreach (var row in rows)
  34. deletes.Add(Items[row.Index]);
  35. Items.RemoveAll(x => deletes.Contains(x));
  36. Selected.RemoveAll(x => deletes.Contains(x));
  37. }
  38. protected override ImportMapping LoadItem(CoreRow row)
  39. {
  40. return Items[row.Index];
  41. }
  42. protected override void Reload(Filters<ImportMapping> criteria, Columns<ImportMapping> columns, ref SortOrder<ImportMapping>? sort,
  43. Action<CoreTable?, Exception?> action)
  44. {
  45. var result = new CoreTable();
  46. result.LoadColumns(typeof(ImportMapping));
  47. if (HideBlank)
  48. result.LoadRows(Items.Where(x => Selected.Contains(x)));
  49. else
  50. result.LoadRows(Items);
  51. action.Invoke(result, null);
  52. }
  53. public override void SaveItem(ImportMapping item)
  54. {
  55. if (!Items.Contains(item))
  56. Items.Add(item);
  57. }
  58. protected override DynamicGridColumns LoadColumns()
  59. {
  60. return new DynamicGridColumns
  61. {
  62. new() { ColumnName = "Property", Width = 0 }
  63. };
  64. }
  65. public void ClearAll()
  66. {
  67. Selected.Clear();
  68. Selected.AddRange(Items.Where(x => x.Key));
  69. Refresh(false, true);
  70. }
  71. public void SelectAll()
  72. {
  73. Selected.Clear();
  74. Selected.AddRange(Items);
  75. Refresh(false, true);
  76. }
  77. private BitmapImage SelectedImage(CoreRow? arg)
  78. {
  79. if (arg == null)
  80. return selected;
  81. var property = arg.Get<ImportMapping, string>(x => x.Property);
  82. return Selected.Any(x => x.Property.Equals(property)) ? selected : null;
  83. }
  84. private bool SelectedAction(CoreRow? arg)
  85. {
  86. var property = arg?.Get<ImportMapping, string>(x => x.Property);
  87. var items = arg == null ? Items.ToArray() : new[] { Items.Where(x => x.Property.Equals(property)).First() };
  88. foreach (var item in items)
  89. if (Selected.Contains(item) && !item.Key)
  90. Selected.Remove(item);
  91. else
  92. Selected.Add(item);
  93. return true;
  94. }
  95. }
  96. }