StockTreatmentGrid.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Media.Imaging;
  5. using Comal.Classes;
  6. using InABox.Core;
  7. using InABox.DynamicGrid;
  8. using InABox.WPF;
  9. namespace PRSDesktop
  10. {
  11. public class StockTreatmentGrid : DynamicDataGrid<StockHolding>
  12. {
  13. private readonly List<Guid> _selected = new();
  14. private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
  15. public StockTreatmentGrid()
  16. {
  17. Options.Clear();
  18. ActionColumns.Add(new DynamicImageColumn(SelectedImage, SelectedAction) { Position = DynamicActionColumnPosition.End });
  19. }
  20. public Guid Location { get; set; }
  21. public IEnumerable<Guid> Selected => _selected;
  22. protected override DynamicGridColumns LoadColumns()
  23. {
  24. //return base.LoadColumns();
  25. var result = new DynamicGridColumns();
  26. result.Add<StockHolding, double>(x => x.Units, 50, "Qty", "F0", Alignment.MiddleCenter);
  27. result.Add<StockHolding, string>(x => x.Product.Code, 120, "Code", "", Alignment.MiddleLeft);
  28. result.Add<StockHolding, string>(x => x.Product.Name, 0, "Name", "", Alignment.MiddleLeft);
  29. result.Add<StockHolding, string>(x => x.Dimensions.UnitSize, 100, "Size", "", Alignment.MiddleCenter);
  30. result.Add<StockHolding, string>(x => x.Job.JobNumber, 70, "Job #", "", Alignment.MiddleCenter);
  31. return result;
  32. }
  33. private BitmapImage SelectedImage(CoreRow arg)
  34. {
  35. return arg == null
  36. ? tick
  37. : _selected.Contains(arg.Get<StockHolding, Guid>(x => x.ID))
  38. ? tick
  39. : null;
  40. }
  41. private bool SelectedAction(CoreRow arg)
  42. {
  43. if (arg == null)
  44. {
  45. if (_selected.Any())
  46. _selected.Clear();
  47. else
  48. _selected.AddRange(Data.ExtractValues<StockHolding, Guid>(x => x.ID));
  49. }
  50. else
  51. {
  52. var id = arg.Get<StockHolding, Guid>(x => x.ID);
  53. if (_selected.Contains(id))
  54. _selected.Remove(id);
  55. else
  56. _selected.Add(id);
  57. }
  58. return true;
  59. }
  60. protected override void Reload(Filters<StockHolding> criteria, Columns<StockHolding> columns, ref SortOrder<StockHolding> sort,
  61. Action<CoreTable, Exception> action)
  62. {
  63. criteria.Add(new Filter<StockHolding>(x => x.Location.ID).IsEqualTo(Location));
  64. base.Reload(criteria, columns, ref sort, action);
  65. }
  66. }
  67. }