Пример #1
0
        private void AddControlContainers(string bindingName, Type propertyType, AutoFormsOrientation orientation)
        {
            var dict = EnumHelper.ToDictionary(propertyType);

            var inline = false;

            if (_attribute is AutoFormsRadioButtonAttribute rba)
            {
                inline = rba.Inline;
            }

            var g = new RadioGroup
            {
                HorizontalOptions = LayoutOptions.Fill,
                VerticalOptions   = LayoutOptions.StartAndExpand,
            };

            g.SetBinding(
                RadioGroup.SelectedIndexProperty,
                new Binding(bindingName, BindingMode.TwoWay, new EnumConverter(), propertyType));

            var h = new Grid
            {
                HorizontalOptions = LayoutOptions.Fill,
                VerticalOptions   = LayoutOptions.StartAndExpand,
            };

            if (orientation == AutoFormsOrientation.Horizontal)
            {
                Grid.SetRow(g, 1);
                Grid.SetColumn(g, 1);
                Grid.SetRow(h, 0);
                Grid.SetColumn(h, 1);
            }
            else
            {
                Grid.SetRow(g, 1);
                Grid.SetColumn(g, 1);
                Grid.SetRow(h, 1);
                Grid.SetColumn(h, 0);
            }


            foreach (var kvp in dict)
            {
                var radio = new RadioButton
                {
                    HorizontalOptions = inline ? LayoutOptions.End : LayoutOptions.CenterAndExpand,
                    VerticalOptions   = LayoutOptions.CenterAndExpand,
                };

                var lbl = new Label
                {
                    Style                   = LabelStyle,
                    Text                    = kvp.Value,
                    VerticalOptions         = LayoutOptions.CenterAndExpand,
                    VerticalTextAlignment   = TextAlignment.Center,
                    HorizontalOptions       = LayoutOptions.Fill,
                    HorizontalTextAlignment = inline ? TextAlignment.Start : TextAlignment.Center,
                    LineBreakMode           = LineBreakMode.WordWrap,
                    GestureRecognizers      =
                    {
                        new TapGestureRecognizer
                        {
                            Command = new Command(() => radio.SelectItem())
                        }
                    }
                };

                if (_attribute.Orientation == AutoFormsOrientation.Horizontal)
                {
                    if (!inline)
                    {
                        g.ColumnDefinitions.Add(new ColumnDefinition {
                            Width = GridLength.Star
                        });
                        h.ColumnDefinitions.Add(new ColumnDefinition {
                            Width = GridLength.Star
                        });
                        Grid.SetColumn(radio, g.ColumnDefinitions.Count - 1);
                        Grid.SetColumn(lbl, g.ColumnDefinitions.Count - 1);
                    }
                    else
                    {
                        g.ColumnDefinitions.Add(new ColumnDefinition {
                            Width = GridLength.Star
                        });
                        Grid.SetColumn(radio, g.ColumnDefinitions.Count - 1);
                        g.ColumnDefinitions.Add(new ColumnDefinition {
                            Width = GridLength.Star
                        });
                        Grid.SetColumn(lbl, g.ColumnDefinitions.Count - 1);
                    }
                }
                else
                {
                    // have to force them the same height
                    h.RowDefinitions.Add(new RowDefinition {
                        Height = 40
                    });
                    g.RowDefinitions.Add(new RowDefinition {
                        Height = 40
                    });

                    Grid.SetRow(radio, g.RowDefinitions.Count - 1);
                    Grid.SetRow(lbl, g.RowDefinitions.Count - 1);

                    if (!inline)
                    {
                        lbl.HorizontalTextAlignment = TextAlignment.End;
                        radio.HorizontalOptions     = LayoutOptions.StartAndExpand;
                    }
                    else
                    {
                        if (g.ColumnDefinitions.Count < 2)
                        {
                            g.ColumnDefinitions.Add(new ColumnDefinition {
                                Width = GridLength.Auto
                            });
                        }
                        if (g.ColumnDefinitions.Count < 2)
                        {
                            g.ColumnDefinitions.Add(new ColumnDefinition {
                                Width = GridLength.Star
                            });
                        }

                        Grid.SetColumn(radio, 0);
                        Grid.SetColumn(lbl, 1);
                    }
                }

                if (!inline)
                {
                    h.Children.Add(lbl);
                }
                else
                {
                    g.Children.Add(lbl);
                }
                g.Children.Add(radio);
            }

            if (_attribute.Orientation == AutoFormsOrientation.Horizontal)
            {
                SetHorizontalLayoutOptions(g, _attribute.HorizontalControlOptions);
                SetHorizontalLayoutOptions(h, _attribute.HorizontalControlOptions);
            }

            if (_attribute.ControlWidthRequest > 0)
            {
                g.WidthRequest = _attribute.ControlWidthRequest;
                h.WidthRequest = _attribute.ControlWidthRequest;
            }


            var c = Content as Grid;

            c.Children.Add(g);

            if (!_hideHeader && !inline)
            {
                c.Children.Add(h);
            }
        }