private void Init() { SegmentItems.Children.Clear(); SegmentItems.RowDefinitions.Clear(); SegmentItems.ColumnDefinitions.Clear(); SegmentItems.ColumnSpacing = this.SectionSpacing; SegmentItems.RowSpacing = this.SectionSpacing; if (ItemsSource?.Any() != true) { return; } for (int i = 0; i < ItemsSource.Count; i++) { var item = ItemsSource[i]; // Check if selected item.IsSelected = item.IsSelected || (SelectedTag != null && SelectedTag?.ToString() == item.Tag?.ToString()); var view = GetView(item, i); // If selected we display content if (item.IsSelected) { this.MainContent = item.Content; } if (SectionOrientation == StackOrientation.Horizontal) { SegmentItems.ColumnDefinitions.Add(new ColumnDefinition() { Width = SectionHorizontalayout.Alignment == LayoutAlignment.Fill ? GridLength.Star : GridLength.Auto }); } else { SegmentItems.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); } SegmentItems.Children.Add(view); } }
private View GetView(SegmentControlItem item) { // Check if item should be selected var isSelected = item.IsSelected || (SelectedTag != null && SelectedTag?.ToString() == item.Tag?.ToString()); Grid view = new Grid() { Padding = new Thickness(1) }; if (UserBorder) { view.Children.Add(new BoxView { BackgroundColor = isSelected ? SelectedBackgroundColor : SegmentBackgroundColor }); } var label = new Label { Text = item.Text, VerticalOptions = LayoutOptions.Center, HorizontalOptions = TextAlignment, FontSize = Device.GetNamedSize(FontSize, typeof(Label)), TextColor = isSelected ? SelectedTextColor : SegmentTextColor, Margin = TextMargin, InputTransparent = true }; if (!UserBorder) { view.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); view.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); BoxView boxView = new BoxView { HeightRequest = 4, BackgroundColor = Color.Transparent, HorizontalOptions = LayoutOptions.Fill, Color = isSelected ? SelectedBackgroundColor : Color.Transparent }; Grid.SetRow(boxView, 1); Grid.SetRow(label, 0); view.Children.Add(boxView); } view.Children.Add(label); item.IsSelected = isSelected; if (item.IsSelected) { this.MainContent = item.Content; } view.GestureRecognizers.Add(new TapGestureRecognizer() { Command = TapGestureCommand, CommandParameter = item }); return(view); }