Na start przeczytaj różnice pomiędzy ItemTemplate
a ContentTemplate
https://stackoverflow.com/questions/25265266/wpfdifference-between-tabcontrol-itemtemplate-and-tabitem-contenttemplate
jak to zrobisz to szybko ogarniesz, na start pokażę wynik:
pamiętaj, że TabControl przyjmuje listę, tak więc do DataContext
przy ItemTemplate
oraz ContentTemplate
otrzymasz element z listy (tracisz więc dostęp do ViewModelu głównego):
TabControl.ItemTemplate
edytujesz tzw. Header
. Natomiast TabControl.ContentTemplate
edytujesz to, co ma się pokazać po kliknięciu w header. Tak więc w Twoim przypadku filmy powinny być wewnątrz klasy TabItemViewModel
. Zobacz przykład:
public class MainWindowViewModel
{
public MainWindowViewModel()
{
Random r = new Random();
Elements1.Add(new Element1(r.Next(1000)));
Elements1.Add(new Element1(r.Next(1000)));
Elements1.Add(new Element1(r.Next(1000)));
Elements1.Add(new Element1(r.Next(1000)));
Elements1.Add(new Element1(r.Next(1000)));
Elements1.Add(new Element1(r.Next(1000)));
}
public IList<Element1> Elements1 { get; set; } = new ObservableCollection<Element1>();
}
public class Element1
{
public Element1(int num)
{
this.num = num;
}
public string Title { get; private set; } = "Tytuł";
public int num { get; private set; }
}
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="500">
<Grid>
<Grid.DataContext>
<local:MainWindowViewModel x:Name="hViewModel"/>
</Grid.DataContext>
<TabControl ItemsSource="{Binding Path=Elements1}">
<TabControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding Path=Title}"/>
</Border>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=num}"/>
</Grid>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
</Window>
Jeżeli twoja lista filmów będzie taka sama dla każdego elementu TabItemViewModel
wtedy możesz znaleźć swój główny ViewModel
za pomocą FindAncestor
użytego podczas bindingu
lub odwołać się do ViewModelu poprzez ElementName
:
public class MainWindowViewModel
{
public MainWindowViewModel()
{
Random r = new Random();
Elements1.Add(new Element1(r.Next(1000)));
Elements1.Add(new Element1(r.Next(1000)));
Elements1.Add(new Element1(r.Next(1000)));
Elements1.Add(new Element1(r.Next(1000)));
Elements1.Add(new Element1(r.Next(1000)));
Elements1.Add(new Element1(r.Next(1000)));
}
public IList<Element1> Elements1 { get; set; } = new ObservableCollection<Element1>();
public string TEST { get; private set; } = "Ten tekst znajduje sie w MainWindowViewModel";
}
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="500">
<Grid>
<Grid.DataContext>
<local:MainWindowViewModel x:Name="hViewModel"/>
</Grid.DataContext>
<TabControl ItemsSource="{Binding Path=Elements1}">
<TabControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding Path=Title}"/>
</Border>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding ElementName=hViewModel, Path=TEST}"/>
</Grid>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
</Window>
W Twoim przypadku <ItemsControl ItemsSource="{Binding Path=DataContext.movies
Twoim DataContext
jest tutaj TabItemViewModel
więc powstaje błąd powiązania do właściwości movies