I want to dynamically add and remove tabs from a wpf TabControl only via databinding. Here is the example XAML:
<Window x:Name="UI_MainWindow" x:Class="DynamicTabs.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DynamicTabs"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid DataContext="{Binding ElementName=UI_MainWindow, Mode=OneWay}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button Content="Add Tab" Click="Click_AddTab"/>
</StackPanel>
<TabControl x:Name="UI_TabControl" Grid.Row="1" ItemsSource="{Binding Tabs}" SelectedItem="{Binding SelectedTab}"/>
</Grid>
</Window>
And here the code behind:
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace DynamicTabs
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(name));
}
public List<TabItem> Tabs { get; set; }
public TabItem SelectedTab { get; set; }
public MainWindow()
{
InitializeComponent();
Tabs=new List<TabItem>();
}
private void Click_AddTab(object sender,RoutedEventArgs e)
{
TabItem newTab=new TabItem();
newTab.Header="Tab"+(Tabs.Count+1).ToString();
Tabs.Add(newTab);
OnPropertyChanged(nameof(Tabs));
SelectedTab=newTab;
OnPropertyChanged(nameof(SelectedTab));
}
}
}
The behaviour is like this: First click on "Add Tab": one tab is shown with header "Tab1" Any further clicks on "Add Tab": the items of the TabControl are increased, also the SelectedItem seems to be updated, but no new headers are drawn.
I tried a workaround (see https://www.codeproject.com/Articles/493538/Add-Remove-Tabs-Dynamically-in-WPF) by setting the DataContext of the TabControl programmatically, which works. However, i would like to understand, why my version does not work.