MobileGridColumn.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. VerticalTextAlignment = TextAlignment.Center,
  36. HorizontalTextAlignment = Alignment,
  37. };
  38. return label;
  39. }
  40. private class MobileGridColumnConverter : IValueConverter
  41. {
  42. private Func<object,object> OnFormatValue;
  43. public MobileGridColumnConverter(Func<object,object> onformatvalue)
  44. {
  45. OnFormatValue = onformatvalue;
  46. }
  47. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  48. {
  49. return OnFormatValue?.Invoke(value);
  50. }
  51. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  52. {
  53. throw new NotImplementedException();
  54. }
  55. }
  56. public TColumn CreateColumn<TColumn>() where TColumn : GridColumn, new()
  57. {
  58. var result = new TColumn();
  59. result.MappingName = ColumnName;
  60. result.DisplayBinding =
  61. new Binding(ColumnName, BindingMode.OneWay, new MobileGridColumnConverter(FormatValue));
  62. if (Width.IsStar)
  63. result.ColumnSizer = ColumnSizer.Star;
  64. else if (Width.IsAuto)
  65. result.ColumnSizer = ColumnSizer.Auto;
  66. else
  67. {
  68. result.ColumnSizer = ColumnSizer.None;
  69. result.Width = Width.Value;
  70. }
  71. result.TextAlignment = Alignment;
  72. result.HeaderTextAlignment = Alignment;
  73. result.HeaderText = Caption;
  74. result.Padding = 0;
  75. result.CellTextSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label));
  76. result.HeaderTemplate = new DataTemplate(() =>
  77. {
  78. var layout = new Grid()
  79. {
  80. VerticalOptions = LayoutOptions.Fill,
  81. RowSpacing = 0,
  82. ColumnSpacing = 0
  83. };
  84. layout.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
  85. layout.RowDefinitions.Add(new RowDefinition() { Height = 1 });
  86. var content = HeaderContent();
  87. content.SetValue(Grid.RowProperty,0);
  88. layout.Children.Add(content);
  89. var box = new BoxView()
  90. {
  91. HeightRequest = 1,
  92. BackgroundColor = Color.LightGray,
  93. Margin = 0
  94. };
  95. box.SetValue(Grid.RowProperty,1);
  96. layout.Children.Add(box);
  97. return layout;
  98. });
  99. return result;
  100. }
  101. protected virtual object FormatValue(object value)
  102. {
  103. return value;
  104. }
  105. }
  106. }