private void InitDirection(StackLayout radio, Label radioText, SelectionFrame radioCircle) { if (Direction == Directions.RTL) { radioText.HorizontalOptions = LayoutOptions.EndAndExpand; radioCircle.HorizontalOptions = LayoutOptions.StartAndExpand; radio.Children.Add(radioCircle); radio.Children.Add(radioText); } else { radioCircle.HorizontalOptions = LayoutOptions.EndAndExpand; radio.Children.Add(radioText); radio.Children.Add(radioCircle); } parentStack.Children.Add(radio); }
private void RebuildOnItemsSource() { if (parentStack.Children.Count > 0) { return; } parentStack.Orientation = Orientation; parentStack.HorizontalOptions = Orientation == StackOrientation.Vertical ? LayoutOptions.Center : HorizontalOptions; parentStack.VerticalOptions = VerticalOptions; var items = ItemsSource; if (Orientation == StackOrientation.Horizontal && Direction == Directions.RTL) { items = items.Reverse(); } foreach (var item in items) { StackLayout radio = new StackLayout { Orientation = StackOrientation.Horizontal, BindingContext = item, HorizontalOptions = Orientation == StackOrientation.Horizontal ? LayoutOptions.CenterAndExpand : LayoutOptions.Fill, }; TapGestureRecognizer tap = new TapGestureRecognizer(); tap.Tapped += RadioChecked; radio.GestureRecognizers.Add(tap); var displayText = DisplayMemberPath == null?item.ToString() : item.GetType().GetProperty(DisplayMemberPath).GetValue(item, null).ToString(); Label radioText = new Label { Text = displayText, VerticalOptions = LayoutOptions.Center, TextColor = FrontColor, FontAttributes = FontAttributes, FontSize = FontSize, FontFamily = FontFamily }; SelectionFrame circle = new SelectionFrame { ClassId = "r", BorderColor = FrontColor, VerticalOptions = LayoutOptions.Center }; InitDirection(radio, radioText, circle); } lbRadios = parentStack.Children.Where(x => x is StackLayout).SelectMany(x => ((StackLayout)x).Children.Where(l => l.ClassId == "r").Cast <SelectionFrame>()).ToList(); if (Orientation == StackOrientation.Horizontal && Direction == Directions.RTL) { lbRadios.Reverse(); } if (SelectedIndex >= 0) { try { lbRadios[SelectedIndex].IsSelected = true; } catch (ArgumentOutOfRangeException) { SetValue(SelectedIndexProperty, 0); lbRadios[SelectedIndex].IsSelected = true; } } }