MobileGridColumn.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using InABox.Core;
  6. using JetBrains.Annotations;
  7. using Syncfusion.SfDataGrid.XForms;
  8. using Xamarin.Forms;
  9. namespace InABox.Mobile
  10. {
  11. public abstract class MobileGridColumn : IMobileGridColumn
  12. {
  13. [CanBeNull]
  14. public static IMobileGridColumn Create(Type entityType, String columnName)
  15. {
  16. var prop = CoreUtils.GetProperty(entityType, columnName);
  17. var baseType = CoreUtils.TypeList(
  18. new[] { typeof(MobileGridColumn).Assembly },
  19. x=>x.IsSubclassOf(typeof(MobileGridColumn))
  20. && !x.IsAbstract
  21. && x.BaseType?.GenericTypeArguments.Contains(prop.PropertyType) == true
  22. ).FirstOrDefault();
  23. if (baseType != null)
  24. {
  25. var resultType = baseType.MakeGenericType(entityType);
  26. var result = (IMobileGridColumn)Activator.CreateInstance(resultType);
  27. result.ColumnName = columnName;
  28. return result;
  29. }
  30. return null;
  31. }
  32. public abstract Action<IMobileGridColumn, object> Tapped { get; set; }
  33. public abstract string ColumnName { get; set; }
  34. public abstract GridLength Width { get; set; }
  35. public abstract string Caption { get; set; }
  36. public abstract TextAlignment Alignment { get; set; }
  37. public abstract GridColumn CreateColumn();
  38. }
  39. public abstract class MobileGridColumn<TEntity, TType> : MobileGridColumn
  40. {
  41. private Expression<Func<TEntity,TType>> _column;
  42. public Expression<Func<TEntity, TType>> Column
  43. {
  44. get => _column;
  45. set
  46. {
  47. _column = value;
  48. _columnName = Column?.ToString().Replace(" ","").Replace("x=>x.","") ?? "";
  49. }
  50. }
  51. private String _columnName;
  52. public override String ColumnName
  53. {
  54. get => _columnName;
  55. set
  56. {
  57. _columnName = value;
  58. _column = CoreUtils.CreateLambdaExpression<TEntity, TType>(value);
  59. }
  60. }
  61. public override GridLength Width { get; set; } = GridLength.Star;
  62. public override String Caption { get; set; }
  63. public override TextAlignment Alignment { get; set; } = TextAlignment.Center;
  64. public override Action<IMobileGridColumn, object> Tapped { get; set; }
  65. protected virtual View HeaderContent()
  66. {
  67. var label = new Label()
  68. {
  69. Text = String.IsNullOrWhiteSpace(Caption)
  70. ? ColumnName
  71. : Caption,
  72. Margin = new Thickness(2,0),
  73. FontSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label)),
  74. TextColor = Color.Black,
  75. VerticalTextAlignment = TextAlignment.Center,
  76. HorizontalTextAlignment = Alignment,
  77. };
  78. return label;
  79. }
  80. private class MobileGridColumnConverter : IValueConverter
  81. {
  82. private readonly Func<object,object> OnFormatValue;
  83. public MobileGridColumnConverter(Func<object,object> onformatvalue)
  84. {
  85. OnFormatValue = onformatvalue;
  86. }
  87. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  88. {
  89. return OnFormatValue?.Invoke(value);
  90. }
  91. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  92. {
  93. throw new NotImplementedException();
  94. }
  95. }
  96. public TColumn CreateColumn<TColumn>() where TColumn : GridColumn, new()
  97. {
  98. var result = new TColumn();
  99. result.MappingName = ColumnName;
  100. result.DisplayBinding =
  101. new Binding(ColumnName, BindingMode.OneWay, new MobileGridColumnConverter(FormatValue));
  102. if (Width.IsStar)
  103. result.ColumnSizer = ColumnSizer.Star;
  104. else if (Width.IsAuto)
  105. result.ColumnSizer = ColumnSizer.Auto;
  106. else
  107. {
  108. result.ColumnSizer = ColumnSizer.None;
  109. result.Width = Width.Value;
  110. }
  111. result.TextAlignment = Alignment;
  112. result.CellStyle.Setters.Add(new Setter() { Property = GridCell.ForegroundProperty, Value = Color.Black });
  113. result.HeaderTextAlignment = Alignment;
  114. result.HeaderText = Caption;
  115. result.Padding = 0;
  116. result.CellTextSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label));
  117. result.HeaderTemplate = new DataTemplate(() =>
  118. {
  119. var layout = new Grid()
  120. {
  121. VerticalOptions = LayoutOptions.Fill,
  122. RowSpacing = 0,
  123. ColumnSpacing = 0
  124. };
  125. layout.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
  126. layout.RowDefinitions.Add(new RowDefinition() { Height = 1 });
  127. var content = HeaderContent();
  128. content.SetValue(Grid.RowProperty,0);
  129. layout.Children.Add(content);
  130. var box = new BoxView()
  131. {
  132. HeightRequest = 1,
  133. BackgroundColor = Color.LightGray,
  134. Margin = 0
  135. };
  136. box.SetValue(Grid.RowProperty,1);
  137. layout.Children.Add(box);
  138. return layout;
  139. });
  140. return result;
  141. }
  142. protected virtual object FormatValue(object value)
  143. {
  144. return value;
  145. }
  146. }
  147. }