MobileGridColumn.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Globalization;
  3. using System.Linq.Expressions;
  4. using Syncfusion.SfDataGrid.XForms;
  5. using Syncfusion.SfDataGrid.XForms.DataPager;
  6. using Xamarin.Forms;
  7. using XF.Material.Forms.Resources.Typography;
  8. using XF.Material.Forms.UI;
  9. namespace InABox.Mobile
  10. {
  11. public abstract class MobileGridColumn<TEntity, TType> : IMobileGridColumn
  12. {
  13. public MobileGridColumn()
  14. {
  15. Alignment = TextAlignment.Center;
  16. Width = GridLength.Star;
  17. }
  18. public Expression<Func<TEntity,TType>> Column { get; set; }
  19. // This should come from CoreUtils
  20. public String ColumnName => Column?.ToString().Replace(" ","").Replace("x=>x.","") ?? "";
  21. public GridLength Width { get; set; }
  22. public String Caption { get; set; }
  23. public TextAlignment Alignment { get; set; }
  24. public Action<IMobileGridColumn, object> Tapped { get; set; }
  25. public abstract GridColumn CreateColumn();
  26. protected virtual View HeaderContent()
  27. {
  28. var label = new Label()
  29. {
  30. Text = String.IsNullOrWhiteSpace(Caption)
  31. ? ColumnName
  32. : Caption,
  33. Margin = new Thickness(2,0),
  34. FontSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label)),
  35. TextColor = Color.Black,
  36. VerticalTextAlignment = TextAlignment.Center,
  37. HorizontalTextAlignment = Alignment,
  38. };
  39. return label;
  40. }
  41. private class MobileGridColumnConverter : IValueConverter
  42. {
  43. private Func<object,object> OnFormatValue;
  44. public MobileGridColumnConverter(Func<object,object> onformatvalue)
  45. {
  46. OnFormatValue = onformatvalue;
  47. }
  48. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  49. {
  50. return OnFormatValue?.Invoke(value);
  51. }
  52. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  53. {
  54. throw new NotImplementedException();
  55. }
  56. }
  57. public TColumn CreateColumn<TColumn>() where TColumn : GridColumn, new()
  58. {
  59. var result = new TColumn();
  60. result.MappingName = ColumnName;
  61. result.DisplayBinding =
  62. new Binding(ColumnName, BindingMode.OneWay, new MobileGridColumnConverter(FormatValue));
  63. if (Width.IsStar)
  64. result.ColumnSizer = ColumnSizer.Star;
  65. else if (Width.IsAuto)
  66. result.ColumnSizer = ColumnSizer.Auto;
  67. else
  68. {
  69. result.ColumnSizer = ColumnSizer.None;
  70. result.Width = Width.Value;
  71. }
  72. result.TextAlignment = Alignment;
  73. result.CellStyle.Setters.Add(new Setter() { Property = GridCell.ForegroundProperty, Value = Color.Black });
  74. result.HeaderTextAlignment = Alignment;
  75. result.HeaderText = Caption;
  76. result.Padding = 0;
  77. result.CellTextSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label));
  78. result.HeaderTemplate = new DataTemplate(() =>
  79. {
  80. var layout = new Grid()
  81. {
  82. VerticalOptions = LayoutOptions.Fill,
  83. RowSpacing = 0,
  84. ColumnSpacing = 0
  85. };
  86. layout.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
  87. layout.RowDefinitions.Add(new RowDefinition() { Height = 1 });
  88. var content = HeaderContent();
  89. content.SetValue(Grid.RowProperty,0);
  90. layout.Children.Add(content);
  91. var box = new BoxView()
  92. {
  93. HeightRequest = 1,
  94. BackgroundColor = Color.LightGray,
  95. Margin = 0
  96. };
  97. box.SetValue(Grid.RowProperty,1);
  98. layout.Children.Add(box);
  99. return layout;
  100. });
  101. return result;
  102. }
  103. protected virtual object FormatValue(object value)
  104. {
  105. return value;
  106. }
  107. }
  108. }