123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 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<TEntity, TType> : IMobileGridColumn
- {
-
- public MobileGridColumn()
- {
- Alignment = TextAlignment.Center;
- Width = GridLength.Star;
- }
-
- public Expression<Func<TEntity,TType>> 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<IMobileGridColumn, object> 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)),
- TextColor = Color.Black,
- VerticalTextAlignment = TextAlignment.Center,
- HorizontalTextAlignment = Alignment,
- };
- return label;
- }
- private class MobileGridColumnConverter : IValueConverter
- {
- private Func<object,object> OnFormatValue;
- public MobileGridColumnConverter(Func<object,object> 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<TColumn>() 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.CellStyle.Setters.Add(new Setter() { Property = GridCell.ForegroundProperty, Value = Color.Black });
-
- 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;
- }
- }
- }
|