DynamicTextColumn.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Linq.Expressions;
  3. using InABox.Core;
  4. namespace InABox.DynamicGrid;
  5. public class DynamicTextColumn : DynamicActionColumn
  6. {
  7. public delegate object? GetTextDelegate(CoreRow? row);
  8. public GetTextDelegate Text { get; protected set; }
  9. public Alignment Alignment { get; set; }
  10. public string? Format { get; set; }
  11. protected DynamicTextColumn()
  12. {
  13. Alignment = Alignment.MiddleCenter;
  14. VerticalHeader = false;
  15. }
  16. public DynamicTextColumn(GetTextDelegate text, ActionDelegate? action = null) : this()
  17. {
  18. Text = text;
  19. Action = action;
  20. }
  21. public override object? Data(CoreRow? row)
  22. {
  23. var result = Text?.Invoke(row);
  24. if (!string.IsNullOrWhiteSpace(Format))
  25. return String.Format($"{{0:{Format}}}", result);
  26. return result;
  27. }
  28. }
  29. public class DynamicTextColumn<T> : DynamicTextColumn
  30. {
  31. public Expression<Func<T, object>> Property { get; set; }
  32. public DynamicTextColumn(Expression<Func<T, object>> property, ActionDelegate? action = null)
  33. {
  34. Text = GetText;
  35. Action = action;
  36. Property = property;
  37. }
  38. private object GetText(CoreRow? row)
  39. {
  40. return row?.Get(Property) ?? "";
  41. }
  42. }