View CreateControlView( PropertyInfo property, AutoFormsListItemAttribute attribute, ControlList listControl) { Style style = null; if (string.IsNullOrEmpty(attribute.ItemStyle) == false && Application.Current.Resources.TryGetValue(attribute.ItemStyle, out object obj)) { style = (Style)obj; } View v = null; switch (attribute.Type) { case AutoFormsType.DateTime: v = new DatePicker { Style = style }; v.SetBinding(DatePicker.DateProperty, new Binding(property.Name, BindingMode.TwoWay, new DateTimeConverter(), property.PropertyType)); break; case AutoFormsType.Entry: var maxLength = property.GetAttribute <AutoFormsMaxLengthAttribute>()?.Length ?? 0; InputView iv; if (attribute.HeightRequest > 0) { iv = new Editor { Style = style, Placeholder = ControlBase.GetLocalizedString(attribute.Placeholder), HeightRequest = attribute.HeightRequest, }; iv.SetBinding(Editor.TextProperty, new Binding(property.Name, BindingMode.TwoWay)); } else { iv = new Entry { Style = style, Placeholder = ControlBase.GetLocalizedString(attribute.Placeholder), }; iv.SetBinding(Entry.TextProperty, new Binding(property.Name, BindingMode.TwoWay)); } if (maxLength > 0) { iv.MaxLength = maxLength; } v = iv; break; case AutoFormsType.Checkbox: //v = new Checkbox //{ // HorizontalOptions = LayoutOptions.Center, //}; //v.SetBinding(Checkbox.CheckedProperty, new Binding(property.Name, BindingMode.TwoWay)); break; case AutoFormsType.Button: var btnAttrib = property.GetAttribute <AutoFormsButtonAttribute>(); if (btnAttrib != null && string.IsNullOrEmpty(btnAttrib.ButtonStyle) == false && Application.Current.Resources.TryGetValue(btnAttrib.ButtonStyle, out object btnObj)) { style = (Style)btnObj; } v = new Button { Style = style, Text = ControlBase.GetLocalizedString(btnAttrib?.Text), HorizontalOptions = LayoutOptions.CenterAndExpand, VerticalOptions = LayoutOptions.CenterAndExpand, }; v.SetBinding(Button.CommandParameterProperty, new Binding(".")); v.SetBinding(Button.CommandProperty, new Binding(property.Name)); break; case AutoFormsType.Combo: v = new Picker(); var propertyType = property.PropertyType; var t = Nullable.GetUnderlyingType(propertyType); if (t != null && t.IsEnum) { propertyType = t; var dict = EnumHelper.ToDictionary(propertyType); var items = new List <ControlCombo.EnumItem>(); items.Add(new ControlCombo.EnumItem { Title = "Please Select", Value = -1 }); int index = 0; foreach (var d in dict) { items.Add(new ControlCombo.EnumItem { Title = d.Value, Value = index++ }); } var picker = v as Picker; picker.ItemsSource = items; picker.SetBinding( Picker.SelectedIndexProperty, new Binding(property.Name, BindingMode.TwoWay, new ControlCombo.EnumItemConverter(), propertyType)); } else if (property.PropertyType.IsEnum) { var picker = v as Picker; var dict = EnumHelper.ToDictionary(property.PropertyType); picker.ItemsSource = dict.Values.ToList(); picker.SetBinding( Picker.SelectedIndexProperty, new Binding(property.Name, BindingMode.TwoWay, new EnumConverter(), property.PropertyType)); } break; default: style = style ?? listControl?.LabelStyle ?? null; v = new Label { Style = style, VerticalOptions = LayoutOptions.CenterAndExpand, VerticalTextAlignment = TextAlignment.Center, HorizontalOptions = LayoutOptions.Fill, HorizontalTextAlignment = attribute.HorizontalItemAlignment, LineBreakMode = LineBreakMode.TailTruncation, MaxLines = 1, }; v.SetBinding(Label.TextProperty, new Binding(property.Name, converter: new DisplayConverter())); break; } return(v); }
protected void RecreatControl() { _grid.Children.Clear(); _grid.RowDefinitions.Clear(); _grid.ColumnDefinitions.Clear(); ControlValidation = null; var listControl = GetParentListView(); if (listControl == null) { return; } var domainModelType = listControl.GetListItemType(); _grid.ColumnSpacing = 0; _grid.Padding = new Thickness(25, 5, 25, 5); var props = AttributeHelper.GetPropertyAttributes <AutoFormsListItemAttribute>(domainModelType); ControlValidation controlValidation = new ControlValidation(() => { _grid.ForceLayout(); }); foreach (var p in props) { var property = p.Item1; var attribute = ControlBase.GetFilteredAttribute(listControl.Filter, p.Item2); if (property == null || attribute == null) { continue; } _grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(attribute.Value, attribute.GridType) }); var v = CreateControlView(property, attribute, listControl); Grid.SetColumn(v, _grid.ColumnDefinitions.Count - 1); _grid.Children.Add(v); var validations = property.GetAttributes <AutoFormsValidationAttribute>(); if (validations != null && validations.Length > 0) { foreach (var validation in validations) { controlValidation.AddValidation(validation, v, string.IsNullOrWhiteSpace(attribute.Label) ? attribute.Placeholder : attribute.Label); } } } CreateActionButtons(listControl); if (controlValidation.Size > 0) { _grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); Grid.SetRow(controlValidation, 1); Grid.SetColumnSpan(controlValidation, _grid.ColumnDefinitions.Count); _grid.Children.Add(controlValidation); ControlValidation = controlValidation; } //_grid.DebugGrid(Color.Red); }