MobileGridNumericColumn.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Linq;
  3. using Syncfusion.Data.Extensions;
  4. using Syncfusion.SfDataGrid.XForms;
  5. using Xamarin.Forms;
  6. namespace InABox.Mobile
  7. {
  8. public class MobileGridNumericConverter : AbstractConverter<double, object>
  9. {
  10. protected override object Convert(double value, object? parameter = null)
  11. {
  12. return value;
  13. }
  14. }
  15. public abstract class MobileGridNumericColumn<TEntity, TType> : MobileGridColumn<TEntity, TType>
  16. where TType : struct, IComparable, IComparable<TType>, IConvertible, IEquatable<TType>, IFormattable
  17. {
  18. public MobileGridSummaryType Summary { get; set; }
  19. public bool BlankIfZero { get; set; }
  20. public String Format { get; set; }
  21. public MobileGridNumericColumn() : base()
  22. {
  23. Width = GridLength.Auto;
  24. BlankIfZero = true;
  25. }
  26. private bool IsIntegral()
  27. {
  28. return new Type[]
  29. {
  30. typeof(sbyte), typeof(SByte),
  31. typeof(byte), typeof(Byte),
  32. typeof(short), typeof(Int16),
  33. typeof(ushort), typeof(UInt16),
  34. typeof(int), typeof(Int32),
  35. typeof(uint), typeof(UInt32),
  36. typeof(long), typeof(Int64),
  37. typeof(ulong), typeof(UInt64)
  38. }
  39. .Contains(typeof(TType));
  40. }
  41. public GridSummaryColumn CreateSummaryColumn()
  42. {
  43. if (Summary == MobileGridSummaryType.None)
  44. return null;
  45. var result = new GridSummaryColumn();
  46. result.SummaryType = IsIntegral()
  47. ? Syncfusion.Data.SummaryType.Int32Aggregate
  48. : Syncfusion.Data.SummaryType.DoubleAggregate;
  49. result.Format = Summary == MobileGridSummaryType.Count
  50. ? "{}{Count}"
  51. : "{}{Sum}";
  52. result.MappingName = ColumnName;
  53. return result;
  54. }
  55. protected override object FormatValue(object value)
  56. {
  57. if (BlankIfZero && Equals(value, default(TType)))
  58. return "";
  59. return String.IsNullOrWhiteSpace(Format)
  60. ? value.ToString()
  61. : String.Format($"{{0:{Format}}}", value);
  62. }
  63. }
  64. }