JobFormGrid.cs 2.3 KB

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