DynamicGridEditorColumn.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Windows;
  5. using System.Windows.Data;
  6. using InABox.Core;
  7. using NPOI.OpenXmlFormats.Wordprocessing;
  8. using Syncfusion.UI.Xaml.Grid;
  9. namespace InABox.DynamicGrid;
  10. public abstract class DynamicGridEditorColumn<TEntity, TEditor, TColumn> : IDynamicGridEditorColumn
  11. where TEntity : BaseObject
  12. where TEditor : class
  13. where TColumn : GridColumn, new()
  14. {
  15. public TColumn Column { get; }
  16. public List<string> ExtraColumns { get; } = new();
  17. GridColumn IDynamicGridEditorColumn.Column => this.Column;
  18. public string? MappingName => _definition?.ColumnName.Replace('.', '_');
  19. public Func<BaseObject>? GetEntity { get; set; }
  20. public Action<Dictionary<string, object?>>? EntityChanged { get; set; }
  21. protected void UpdateData(Dictionary<string, object> updates)
  22. {
  23. var entity = GetEntity?.Invoke();
  24. if (entity != null)
  25. {
  26. var changes = new Dictionary<string, object?>();
  27. foreach (var key in updates.Keys)
  28. DynamicGridUtils.UpdateEditorValue(new BaseObject[] { entity }, key, updates[key], changes);
  29. EntityChanged?.Invoke(changes);
  30. }
  31. }
  32. protected DynamicGridEditorColumn(DynamicGridColumn definition)
  33. {
  34. Column = new TColumn()
  35. {
  36. UpdateTrigger = UpdateSourceTrigger.PropertyChanged
  37. };
  38. Definition = definition;
  39. }
  40. private DynamicGridColumn? _definition;
  41. public DynamicGridColumn? Definition
  42. {
  43. get => _definition;
  44. set
  45. {
  46. _definition = value;
  47. UpdateBinding(Column);
  48. if (Editor != null)
  49. DoConfigure(Column, Editor);
  50. }
  51. }
  52. public virtual GridSummaryColumn? Summary() => null;
  53. public TEditor? Editor
  54. {
  55. get
  56. {
  57. if (Definition != null)
  58. {
  59. var result = Definition?.Editor as TEditor;
  60. if (result == null)
  61. result = CoreUtils.GetProperty<TEntity>(Definition.ColumnName)?.GetEditor() as TEditor;
  62. return result;
  63. }
  64. return null;
  65. }
  66. }
  67. protected virtual void UpdateBinding(TColumn column)
  68. {
  69. column.MappingName = MappingName;
  70. }
  71. protected TextAlignment GetTextAlignment(TEditor editor) => Definition?.Alignment == Alignment.NotSet
  72. ? editor is NumericEditor ? TextAlignment.Right : TextAlignment.Left
  73. : Definition?.Alignment == Alignment.BottomLeft ||
  74. Definition?.Alignment == Alignment.MiddleLeft ||
  75. Definition?.Alignment == Alignment.TopLeft
  76. ? TextAlignment.Left
  77. : Definition?.Alignment == Alignment.BottomCenter ||
  78. Definition?.Alignment == Alignment.MiddleCenter ||
  79. Definition?.Alignment == Alignment.TopCenter
  80. ? TextAlignment.Center
  81. : TextAlignment.Right;
  82. private void DoConfigure(TColumn column, TEditor editor)
  83. {
  84. Configure(column,editor);
  85. // Post-Configure the column - refactor from DynamicGrid.ReloadColumns()
  86. Column.Width = column.Width; // != 0 ? column.Width : double.NaN;
  87. Column.ColumnSizer =
  88. GridLengthUnitType
  89. .None; //column.Width != 0 ? GridLengthUnitType.None : GridLengthUnitType.AutoWithLastColumnFill;
  90. Column.HeaderText = string.IsNullOrWhiteSpace(Definition?.Caption)
  91. ? Definition?.ColumnName
  92. : Definition.Caption;
  93. Column.TextAlignment = GetTextAlignment(editor);
  94. Column.HorizontalHeaderContentAlignment = Column.TextAlignment == TextAlignment.Left
  95. ? HorizontalAlignment.Left
  96. : Column.TextAlignment == TextAlignment.Center
  97. ? HorizontalAlignment.Center
  98. : HorizontalAlignment.Right;
  99. }
  100. protected abstract void Configure(TColumn column, TEditor editor);
  101. public virtual bool VariableWidth => true;
  102. public virtual bool VariableHeight => false;
  103. public virtual bool Filtered => true;
  104. public virtual bool Editable => Definition?.Editor.Editable == InABox.Core.Editable.Enabled;
  105. }