DynamicExportMappingGrid.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 = Properties.Resources.Bullet_Tick.AsBitmapImage();
  12. public DynamicExportMappingGrid()
  13. {
  14. HideBlank = false;
  15. Items = new List<ImportMapping>();
  16. Selected = new List<ImportMapping>();
  17. //Options.AddRange(DynamicGridOption.RecordCount);
  18. ActionColumns.Add(new DynamicActionColumn(SelectedImage, SelectedAction));
  19. HiddenColumns.Add(x => x.Key);
  20. }
  21. public List<ImportMapping> Items { get; }
  22. public List<ImportMapping> Selected { get; }
  23. public bool HideBlank { get; set; }
  24. protected override void DeleteItems(params CoreRow[] rows)
  25. {
  26. var deletes = new List<ImportMapping>();
  27. foreach (var row in rows)
  28. deletes.Add(Items[row.Index]);
  29. Items.RemoveAll(x => deletes.Contains(x));
  30. Selected.RemoveAll(x => deletes.Contains(x));
  31. }
  32. protected override ImportMapping LoadItem(CoreRow row)
  33. {
  34. return Items[row.Index];
  35. }
  36. protected override void Reload(Filters<ImportMapping> criteria, Columns<ImportMapping> columns, ref SortOrder<ImportMapping> sort,
  37. 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. protected 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. }
  90. }