Trying to build a TabControl dynamically, but it won't bind the UserControl I've created. I've extended TabItem to add some close functionality to the header.
The error it gives is:
System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='ClosableTab'
I end up with System.Windows.Controls.Grid in plain text rather than a control. The UserControl is just containing a TextBlock in an empty grid for testing purposes.
The error is remarkably similar to this question: Data Error 26 with DataTemplate and ItemTemplate but my code is different and I can't figure out if it's the same, but I have tried to search.
And this code contains is sourced from multiple tutorials, I'm still only a month in so sorry if I've missed something obvious.
<TabControl TabStripPlacement="Top"
ItemsSource="{Binding Titles, Mode=TwoWay}"
Grid.Column="3" Grid.Row="1">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding Content}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
The extended TabItem class. TabHeader is just Xaml to add a close button to the header, works fine. {Binding Content} does show a warning,
Cannot resolve property 'Content' in data context of type MyApp.ViewModel.MainWindowViewModel
Perhaps this is key?
public class ClosableTab : TabItem
{
public ClosableTab(string headerText, UserControl content)
{
TabHeader header = new TabHeader();
Header = header;
Title = headerText;
Content = content;
}
public string Title
{
get { return Title.ToString(); }
set
{
((TabHeader)this.Header).label_TabTitle.Content = value;
}
}
}
The ICommand event handler
private ObservableCollection<ClosableTab> _titles;
public ObservableCollection<ClosableTab> Titles
{
get { return _titles; }
set
{
_titles = value;
OnPropertyChanged("Titles");
}
}
private void AddTabItem(string title)
{
ClosableTab Item;
Item = new ClosableTab("Setup", new Setup());
Titles.Add(Item);
Item.Focus();
OnPropertyChanged("Titles");
}
New Setup() is a user control containing just a Grid and a TextBlock.
Everything works perfectly, the header is built with the title and close button, but the content just returns text, System.Windows.Controls.Grid?