private void UpdateSelectedSegmentLayout(SegmentControlItem item) { // Get old item var oldItem = ItemsSource.FirstOrDefault(x => x.IsSelected); var selectedIndex = ItemsSource.IndexOf(item); var unselectIndex = oldItem == null ? -1 : ItemsSource.IndexOf(oldItem); if (unselectIndex != -1) { oldItem.IsSelected = false; SegmentItems.Children.RemoveAt(unselectIndex); SegmentItems.Children.Insert(unselectIndex, GetView(oldItem, unselectIndex)); } item.IsSelected = true; SegmentItems.Children.RemoveAt(selectedIndex); SegmentItems.Children.Insert(selectedIndex, GetView(item, selectedIndex)); Device.BeginInvokeOnMainThread(() => { this.MainContent = item.Content; }); }
private void UpdateSelectedSegmentLayout(SegmentControlItem item) { // Unmark old value var oldItem = ItemsSource.FirstOrDefault(x => x.IsSelected); if (oldItem != null) { oldItem.IsSelected = false; Grid oldSelectedView = (Grid)segmentControlItems.Children.FirstOrDefault(x => (x as Grid)?.Children?.Any(c => (c as Label)?.Text == oldItem.Text) == true); if (oldSelectedView != null) { if (UserBorder) { oldSelectedView.Children[0].BackgroundColor = SegmentBackgroundColor; (oldSelectedView.Children[1] as Label).TextColor = SegmentTextColor; } else { (oldSelectedView.Children[0] as BoxView).Color = Color.Transparent; (oldSelectedView.Children[1] as Label).TextColor = SegmentTextColor; } } } // Mark new value item.IsSelected = true; Grid newSelectedView = (Grid)segmentControlItems.Children.FirstOrDefault(x => (x as Grid)?.Children?.Any(c => (c as Label)?.Text == item.Text) == true); if (newSelectedView != null) { if (UserBorder) { newSelectedView.Children[0].BackgroundColor = SelectedBackgroundColor; (newSelectedView.Children[1] as Label).TextColor = SelectedTextColor; } else { (newSelectedView.Children[0] as BoxView).Color = SelectedBackgroundColor; (newSelectedView.Children[1] as Label).TextColor = SelectedTextColor; } } Device.BeginInvokeOnMainThread(async() => { await MainContent.FadeTo(0, length: 150); this.MainContent = item.Content; await MainContent.FadeTo(1, length: 150); }); }
private View GetView(SegmentControlItem item, int index) { // Get view View view; if (SectionTemplate != null && SelectedSectionTemplate != null) { view = item.IsSelected ? SelectedSectionTemplate.CreateContent() as View : SectionTemplate.CreateContent() as View; } else { if (SelectedSectionBackground == Color.Transparent) { view = new StackLayout { Padding = SectionPadding, Children = { new Label { FontSize = Device.GetNamedSize(FontSize, typeof(Label)), FontFamily = App.Current.Get <string>("OpenSansSemiBold"), Opacity = item.IsSelected ? 1 : 0.8, Text = item.Text, HorizontalOptions = SectionTextHorizontalLayout, TextColor = item.IsSelected ? SelectedSectionTextColor : SectionTextColor }, new BoxView { Color = item.IsSelected ? SelectedSectionTextColor : Color.Transparent, HeightRequest = 3, HorizontalOptions = LayoutOptions.FillAndExpand, } } }; } else { var stackLayout = new StackLayout { Orientation = item.ItemOrientation, Padding = SectionPadding, HorizontalOptions = LayoutOptions.FillAndExpand, Spacing = item.Spacing, BackgroundColor = item.IsSelected ? SelectedSectionBackground : SectionBackground }; if (!string.IsNullOrEmpty(item.IconSource)) { stackLayout.Children.Add(new Label { Text = item.IconSource, VerticalOptions = LayoutOptions.Center, FontFamily = item.IconFontFamily, TextColor = item.IsSelected ? SelectedSectionTextColor : SectionTextColor, FontSize = Device.GetNamedSize(FontSize, typeof(Label)), InputTransparent = true }); } if (!string.IsNullOrEmpty(item.Text)) { stackLayout.Children.Add(new Label { VerticalOptions = LayoutOptions.Center, HorizontalOptions = SectionTextHorizontalLayout, HorizontalTextAlignment = TextAlignment.Center, FontSize = Device.GetNamedSize(FontSize, typeof(Label)), FontFamily = App.Current.Get <string>("OpenSansSemiBold"), Text = item.Text, Margin = SectionControlMargin, InputTransparent = true, TextColor = item.IsSelected ? SelectedSectionTextColor : SectionTextColor }); } view = stackLayout; } } view.BindingContext = item; // Adding tap gesture view.GestureRecognizers.Add(new TapGestureRecognizer() { Command = TapGestureCommand, CommandParameter = item }); if (SectionOrientation == StackOrientation.Horizontal) { Grid.SetColumn(view, index); } else { Grid.SetRow(view, index); } return(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); }