using System; using System.Globalization; using System.Linq.Expressions; using Syncfusion.SfDataGrid.XForms; using Syncfusion.SfDataGrid.XForms.DataPager; using Xamarin.Forms; using XF.Material.Forms.Resources.Typography; using XF.Material.Forms.UI; namespace InABox.Mobile { public abstract class MobileGridColumn : IMobileGridColumn { public MobileGridColumn() { Alignment = TextAlignment.Center; Width = GridLength.Star; } public Expression> Column { get; set; } // This should come from CoreUtils public String ColumnName => Column?.ToString().Replace(" ","").Replace("x=>x.","") ?? ""; public GridLength Width { get; set; } public String Caption { get; set; } public TextAlignment Alignment { get; set; } public Action Tapped { get; set; } public abstract GridColumn CreateColumn(); protected virtual View HeaderContent() { var label = new Label() { Text = String.IsNullOrWhiteSpace(Caption) ? ColumnName : Caption, Margin = new Thickness(2,0), FontSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label)), VerticalTextAlignment = TextAlignment.Center, HorizontalTextAlignment = Alignment, }; return label; } private class MobileGridColumnConverter : IValueConverter { private Func OnFormatValue; public MobileGridColumnConverter(Func onformatvalue) { OnFormatValue = onformatvalue; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return OnFormatValue?.Invoke(value); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } public TColumn CreateColumn() where TColumn : GridColumn, new() { var result = new TColumn(); result.MappingName = ColumnName; result.DisplayBinding = new Binding(ColumnName, BindingMode.OneWay, new MobileGridColumnConverter(FormatValue)); if (Width.IsStar) result.ColumnSizer = ColumnSizer.Star; else if (Width.IsAuto) result.ColumnSizer = ColumnSizer.Auto; else { result.ColumnSizer = ColumnSizer.None; result.Width = Width.Value; } result.TextAlignment = Alignment; result.HeaderTextAlignment = Alignment; result.HeaderText = Caption; result.Padding = 0; result.CellTextSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label)); result.HeaderTemplate = new DataTemplate(() => { var layout = new Grid() { VerticalOptions = LayoutOptions.Fill, RowSpacing = 0, ColumnSpacing = 0 }; layout.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star }); layout.RowDefinitions.Add(new RowDefinition() { Height = 1 }); var content = HeaderContent(); content.SetValue(Grid.RowProperty,0); layout.Children.Add(content); var box = new BoxView() { HeightRequest = 1, BackgroundColor = Color.LightGray, Margin = 0 }; box.SetValue(Grid.RowProperty,1); layout.Children.Add(box); return layout; }); return result; } protected virtual object FormatValue(object value) { return value; } } }