internal void InitTabs() { Items.Clear(); if (ItemsDataSource != null) { foreach (var item in ItemsDataSource) { var newitem = new TabItem(); if (TabItemTemplate != null) { newitem.Content = TabItemTemplate.LoadContent(); } if (TabHeaderItemTemplate != null) { newitem.Header = TabHeaderItemTemplate.LoadContent(); } newitem.DataContext = item; Items.Add(newitem); } } }
private void AddTabToView(TabItem tab) { var tabSize = (TabSizeOption.IsAbsolute && TabSizeOption.Value.Equals(0)) ? new GridLength(1, GridUnitType.Star) : TabSizeOption; _headerContainerGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = tabSize }); tab.IsCurrent = _headerContainerGrid.ColumnDefinitions.Count - 1 == SelectedTabIndex; var headerIcon = new Image { Margin = new Thickness(0, 8, 0, 0), BindingContext = tab, HorizontalOptions = LayoutOptions.CenterAndExpand, VerticalOptions = LayoutOptions.Center, WidthRequest = tab.HeaderIconSize, HeightRequest = tab.HeaderIconSize }; headerIcon.SetBinding(Image.SourceProperty, nameof(TabItem.HeaderIcon)); headerIcon.SetBinding(IsVisibleProperty, nameof(TabItem.HeaderIcon), converter: new NullToBoolConverter()); var headerLabel = new Label { BindingContext = tab, VerticalTextAlignment = TextAlignment.Center, HorizontalTextAlignment = TextAlignment.Center, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.CenterAndExpand, Margin = new Thickness(5, 8, 5, 5) }; headerLabel.SetBinding(Label.TextProperty, nameof(TabItem.HeaderText)); headerLabel.SetBinding(Label.TextColorProperty, nameof(TabItem.HeaderTextColor)); headerLabel.SetBinding(Label.FontSizeProperty, nameof(TabItem.HeaderTabTextFontSize)); headerLabel.SetBinding(Label.FontFamilyProperty, nameof(TabItem.HeaderTabTextFontFamily)); headerLabel.SetBinding(Label.FontAttributesProperty, nameof(TabItem.HeaderTabTextFontAttributes)); headerLabel.SetBinding(IsVisibleProperty, nameof(TabItem.HeaderText), converter: new NullToBoolConverter()); var selectionBarBoxView = new BoxView { VerticalOptions = LayoutOptions.End, BindingContext = tab, HeightRequest = HeaderSelectionUnderlineThickness, WidthRequest = HeaderSelectionUnderlineWidth }; //selectionBarBoxView.SetBinding(IsVisibleProperty, nameof(TabItem.IsCurrent)); var underlineColorBinding = new Binding(nameof(TabItem.IsCurrent), BindingMode.OneWay, new SelectedTabHeaderToTabBackgroundColorConverter(), this); //selectionBarBoxView.SetBinding(BoxView.ColorProperty, nameof(TabItem.HeaderSelectionUnderlineColor), BindingMode.OneWay, new SelectedTabHeaderToTabBackgroundColorConverter(), ); selectionBarBoxView.SetBinding(BoxView.BackgroundColorProperty, underlineColorBinding); selectionBarBoxView.SetBinding(WidthRequestProperty, nameof(TabItem.HeaderSelectionUnderlineWidth)); selectionBarBoxView.SetBinding(HeightRequestProperty, nameof(TabItem.HeaderSelectionUnderlineThickness)); selectionBarBoxView.SetBinding(HorizontalOptionsProperty, nameof(TabItem.HeaderSelectionUnderlineWidth), converter: new DoubleToLayoutOptionsConverter()); selectionBarBoxView.PropertyChanged += (object sender, PropertyChangedEventArgs e) => { if (e.PropertyName == nameof(TabItem.IsCurrent)) { SetPosition(ItemSource.IndexOf((TabItem)((BoxView)sender).BindingContext)); } if (e.PropertyName == nameof(WidthRequest)) { selectionBarBoxView.HorizontalOptions = tab.HeaderSelectionUnderlineWidth > 0 ? LayoutOptions.CenterAndExpand : LayoutOptions.FillAndExpand; } }; View headerItemView; if (TabHeaderItemTemplate != null) { headerItemView = (View)TabHeaderItemTemplate.CreateContent(); headerItemView.BindingContext = tab; } else { headerItemView = new StackLayout { VerticalOptions = LayoutOptions.FillAndExpand, Children = { headerIcon, headerLabel, selectionBarBoxView }, BackgroundColor = HeaderBackgroundColor }; } var tapRecognizer = new TapGestureRecognizer(); tapRecognizer.Tapped += (object s, EventArgs e) => { _supressCarouselViewPositionChangedEvent = true; var capturedIndex = _headerContainerGrid.Children.IndexOf((View)s); SetPosition(capturedIndex); _supressCarouselViewPositionChangedEvent = false; }; headerItemView.GestureRecognizers.Add(tapRecognizer); _headerContainerGrid.Children.Add(headerItemView, _headerContainerGrid.ColumnDefinitions.Count - 1, 0); _carouselView.ItemsSource = ItemSource.Select(t => t.Content); }
void ItemsDataSource_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) { if (e.NewStartingIndex > -1) { foreach (var item in e.NewItems) { var newitem = new TabItem(); if (TabItemTemplate != null) { newitem.Content = TabItemTemplate.LoadContent(); } if (TabHeaderItemTemplate != null) { newitem.Header = TabHeaderItemTemplate.LoadContent(); } newitem.DataContext = item; Items.Add(newitem); this.SelectedItem = newitem; } } } else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) { if (e.OldStartingIndex > -1) { Items.RemoveAt(e.OldStartingIndex); } } else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace) { Items.RemoveAt(e.OldStartingIndex); var newitem = new TabItem(); if (TabItemTemplate != null) { newitem.Content = TabItemTemplate.LoadContent(); } if (TabHeaderItemTemplate != null) { newitem.Header = TabHeaderItemTemplate.LoadContent(); } newitem.DataContext = e.NewItems[0]; Items.Add(newitem); Items.Insert(e.NewStartingIndex, newitem); } else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset) { InitTabs(); } }