JobFormGrid.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Linq;
  3. using Comal.Classes;
  4. using InABox.Core;
  5. using InABox.DynamicGrid;
  6. using InABox.WPF;
  7. using InABox.Wpf;
  8. namespace PRSDesktop
  9. {
  10. public class JobFormGrid : DynamicDataGrid<JobForm>, IMasterDetailControl<Job,JobForm>, IDataModelSource
  11. {
  12. public Job? Master { get; set; }
  13. public Filter<JobForm> MasterDetailFilter => (Master?.ID ?? Guid.Empty) != Guid.Empty
  14. ? new Filter<JobForm>(x => x.Parent.ID).IsEqualTo(Master.ID)
  15. : new Filter<JobForm>().None();
  16. public event DataModelUpdateEvent? OnUpdateDataModel;
  17. public string SectionName => "Job Forms";
  18. public DataModel DataModel(Selection selection)
  19. {
  20. var ids = ExtractValues(x => x.ID, selection).ToArray();
  21. return new AutoDataModel<JobForm>(new Filter<JobForm>(x => x.ID).InList(ids));
  22. }
  23. public JobFormGrid()
  24. {
  25. ActionColumns.Add(new DynamicImageColumn(PRSDesktop.Resources.pencil.AsBitmapImage(), EditAction));
  26. }
  27. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  28. {
  29. base.DoReconfigure(options);
  30. options
  31. .BeginUpdate()
  32. .Add(DynamicGridOption.SelectColumns)
  33. .Add(DynamicGridOption.FilterRows)
  34. .EndUpdate();
  35. }
  36. private bool EditAction(CoreRow? row)
  37. {
  38. if (row == null) return false;
  39. if(DynamicFormEditWindow.EditDigitalForm<JobForm>(row.Get<JobForm, Guid>(x => x.ID), out var dataModel))
  40. {
  41. dataModel.Update(null);
  42. return true;
  43. }
  44. return false;
  45. }
  46. protected override void Reload(Filters<JobForm> criteria, Columns<JobForm> columns, ref SortOrder<JobForm>? sort,
  47. Action<CoreTable?, Exception?> action)
  48. {
  49. criteria.Add(MasterDetailFilter);
  50. base.Reload(criteria, columns, ref sort, action);
  51. }
  52. protected override bool CanCreateItems()
  53. {
  54. return base.CanCreateItems() && Master != null;
  55. }
  56. public override JobForm CreateItem()
  57. {
  58. var result = base.CreateItem();
  59. result.Parent.ID = Master?.ID ?? Guid.Empty;
  60. result.Parent.Synchronise(Master ?? new Job());
  61. return result;
  62. }
  63. }
  64. }