I am having trouble accessing the correct DataContext
for TabItems
with a dynamically populated TabControl
.
Using the below code i bind the ItemsSource
of my TabControl
to an ObservableCollection<TabViewModel>
on my MainViewModel
.
Based on the Type
property of the each TabViewModel
in the ItemSource
, a different UserControl
is selected as the Content
property of the TabItem
. It is here where this start to go wrong
These UserControls
need to have their DataContext
set to the MainViewModel
, however accessing this is not possible as the dynamic TabItems
seem to be created in a different visual tree.
<TabControl x:Name="mainTabControl" ItemsSource="{Binding TabList}">
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="Header" Value="{Binding Name}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Type}" Value="Summary" >
<Setter Property="Content">
<Setter.Value>
<views:AnalysisArisingSummaryControl />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Type}" Value="User Defined" >
<Setter Property="Content">
<Setter.Value>
<views:AnalysisUserDefinedControl />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Type}" Value="System Defined" >
<Setter Property="Content">
<Setter.Value>
<views:AnalysisSystemDefinedControl />
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
Any ideas as to how to accomplish this. I dont really want to have to reference data between ViewModels
if i can help it.