1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- namespace InABox.Core
- {
- public class CoreGridColumn
- {
- public IProperty Property { get; set; }
- public int Width { get; set; }
- public Alignment Alignment { get; set; }
- public string Format { get; set; }
- public BaseEditor Editor { get; set; }
- public string Caption { get; set; }
- public CoreGridColumn(IProperty property, int width, Alignment alignment, string format, BaseEditor editor, string caption)
- {
- Property = property;
- Width = width;
- Alignment = alignment;
- Format = format;
- Editor = editor;
- Caption = caption;
- }
- }
- /// <summary>
- /// This is the new system for managing default columns on grids. (This replaces <see cref="Visible.Default"/> on <see cref="BaseEditor.Visible"/>).
- /// </summary>
- public static class DefaultColumns
- {
- private static readonly Dictionary<Type, List<CoreGridColumn>> _columns = new Dictionary<Type, List<CoreGridColumn>>();
- public static IEnumerable<CoreGridColumn> GetDefaultColumns(Type T)
- {
- System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(T.TypeHandle);
- return _columns.GetValueOrDefault(T) ?? Enumerable.Empty<CoreGridColumn>();
- }
- public static CoreGridColumn CreateColumn<TType, TProperty>(
- Expression<Func<TType, TProperty>> member,
- int? width = null,
- string? caption = null,
- string? format = null,
- Alignment? alignment = null)
- {
- var prop = DatabaseSchema.Property(member) ?? throw new Exception($"Could not find property {CoreUtils.GetFullPropertyName(member, ".")}");
- var col = new CoreGridColumn(
- prop,
- width ?? prop.Editor.Width,
- alignment ?? prop.Editor.Alignment,
- format ?? prop.Editor.Format,
- prop.Editor.CloneEditor(),
- caption ?? prop.Caption);
- return col;
- }
- public static CoreGridColumn Add<TType>(
- Expression<Func<TType, object?>> member,
- int? width = null,
- string? caption = null,
- string? format = null,
- Alignment? alignment = null
- )
- {
- var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment);
- var list = _columns.GetValueOrAdd(typeof(TType));
- list.Add(col);
- return col;
- }
- }
- }
|