I'm new in WPF and try learn VMMV. I try to create TabControl with template for content in xaml. I want tabitem with content of grid and in grid list of usercontrols. After adding a usercontrol the header of tabitem render right but nothing is in content. What is wrong?
This is my xaml:
<TabControl ItemsSource="{Binding Items}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Visibility="Hidden" Name="tcContent" Grid.Column="1" Grid.Row="0">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ContentControl Content="{Binding Content}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
Viewmodel:
public class Tab
{
public string Header { get; set; }
public ObservableCollection<UserControl> Content { get; set; }
}
public class MainWindowsViewModel
{
ObservableCollection<Tab> _items = new ObservableCollection<Tab>();
public ObservableCollection<Tab> Items
{
get
{
return _items;
}
}
}
Behind code for fill tabcontrol:
public MainWindow()
{
this.DataContext = new MainWindowsViewModel();
}
public void AddToTab(string header, UserControl c)
{
Tab tab = new Tab();
tab.Header = header;
tab.Content = new ObservableCollection<UserControl>();
tab.Content.Add(c);
((MainWindowsViewModel)this.DataContext).Items.Add(tab);
}