protected override void OnPropertyChanging([CallerMemberName] string propertyName = null) { base.OnPropertyChanging(propertyName); if (propertyName == ItemsSourceProperty.PropertyName) { if (ItemsSource != null) { if (ItemsSource is INotifyCollectionChanged itemsSource) { itemsSource.CollectionChanged -= ItemsSource_CollectionChanged; } _useItemsSource = false; Tabs.Clear(); } } else if (propertyName == SelectedTabIndexProperty.PropertyName) { if (SelectedTabIndex == -1) { _prevSelectedTabItem = null; } else { _prevSelectedTabItem = Tabs.ElementAtOrDefault(SelectedTabIndex); } } }
private void InitContentTemplate(object item, TabViewItem tabItem) { if (ContentTemplate != null) { var template = ContentTemplate; if (ContentTemplate is DataTemplateSelector selector) { template = selector.SelectTemplate(item, this); } var tabContent = template.CreateContent() as View; if (tabContent.BindingContext == null) { tabContent.BindingContext = item; } tabItem.Content = tabContent; } else { var tabContent = new Label(); tabContent.Text = item.ToString(); tabItem.Content = tabContent; } }
private void InitItems(IEnumerable source, bool useIndex = false, int index = 0) { if (source == null) { return; } foreach (var item in source) { var tabItem = new TabViewItem(); InitContentTemplate(item, tabItem); tabItem.BindingContext = item; tabItem.Header = item; if (useIndex) { Tabs.Insert(index, tabItem); } else { Tabs.Add(tabItem); } } }
private void SelectClosestTab(TabViewItem tab, List <TabViewItem> tabs) { if (tabs.Count == 1) { SelectedTabIndex = -1; } else { var index = tabs.IndexOf(tab); if (index == tabs.Count - 1) { var tabToSelect = tabs.ElementAtOrDefault(index - 1); SelectedTabIndex = Tabs.IndexOf(tabToSelect); } else if (index >= 0) { var tabToSelect = tabs.ElementAtOrDefault(index + 1); SelectedTabIndex = Tabs.IndexOf(tabToSelect); } } }