DynamicExportMappingGrid.cs 3.4 KB

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