DefaultColumns.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Text;
  7. namespace InABox.Core
  8. {
  9. public class CoreGridColumn
  10. {
  11. public IProperty Property { get; set; }
  12. public int Width { get; set; }
  13. public Alignment Alignment { get; set; }
  14. public string Format { get; set; }
  15. public BaseEditor Editor { get; set; }
  16. public string Caption { get; set; }
  17. public CoreGridColumn(IProperty property, int width, Alignment alignment, string format, BaseEditor editor, string caption)
  18. {
  19. Property = property;
  20. Width = width;
  21. Alignment = alignment;
  22. Format = format;
  23. Editor = editor;
  24. Caption = caption;
  25. }
  26. }
  27. /// <summary>
  28. /// This is the new system for managing default columns on grids. (This replaces <see cref="Visible.Default"/> on <see cref="BaseEditor.Visible"/>).
  29. /// </summary>
  30. public static class DefaultColumns
  31. {
  32. private static readonly Dictionary<Type, List<CoreGridColumn>> _columns = new Dictionary<Type, List<CoreGridColumn>>();
  33. public static IEnumerable<CoreGridColumn> GetDefaultColumns(Type T)
  34. {
  35. System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(T.TypeHandle);
  36. return _columns.GetValueOrDefault(T) ?? Enumerable.Empty<CoreGridColumn>();
  37. }
  38. public static CoreGridColumn CreateColumn<TType, TProperty>(
  39. Expression<Func<TType, TProperty>> member,
  40. int? width = null,
  41. string? caption = null,
  42. string? format = null,
  43. Alignment? alignment = null)
  44. {
  45. var prop = DatabaseSchema.Property(member) ?? throw new Exception($"Could not find property {CoreUtils.GetFullPropertyName(member, ".")}");
  46. var col = new CoreGridColumn(
  47. prop,
  48. width ?? prop.Editor.Width,
  49. alignment ?? prop.Editor.Alignment,
  50. format ?? prop.Editor.Format,
  51. prop.Editor.CloneEditor(),
  52. caption ?? prop.Caption);
  53. return col;
  54. }
  55. public static CoreGridColumn Add<TType>(
  56. Expression<Func<TType, object?>> member,
  57. int? width = null,
  58. string? caption = null,
  59. string? format = null,
  60. Alignment? alignment = null
  61. )
  62. {
  63. var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment);
  64. var list = _columns.GetValueOrAdd(typeof(TType));
  65. list.Add(col);
  66. return col;
  67. }
  68. }
  69. }