I have problem with my app. I use 2 TabControl (main and sub). On my sub control I have 2 TabItems. All it's working but DataGridTextColumn.Header Binding working only for first TabItem. Window DataContext is set to ViewModelClass when window is showed (after click button)
Second TabItem has no column name. Binding error is :
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=DataContext.Translations.NumberText; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
Here is code snippet:
<TabControl x:Name="ovTcMain">
<TabItem Header="{Binding Translations.CodeBooksText }" TabIndex="0">
<Grid Style="{StaticResource MainGrid}">
<Grid.RowDefinitions>
<RowDefinition Height="6*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<TabControl x:Name="ovTcOptions" Grid.Row="0" SelectedIndex="{Binding Path=SelectedIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<TabItem Header="{Binding Translations.ResortsText}" TabIndex="0" >
<DataGrid x:Name="ovTiCostCenters"
ItemsSource="{Binding CodeBooks[CostCenter], Mode=TwoWay}"
SelectedItem="{Binding SelectedCostCenter, Mode=TwoWay}"
ColumnWidth="*" AutoGenerateColumns="False" HorizontalAlignment="Stretch"
Visibility="Visible" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}" Width="auto">
<DataGridTextColumn.Header>
<TextBlock Text="{Binding DataContext.Translations.NumberText, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=DescriptionTerm}" Width="auto">
<DataGridTextColumn.Header>
<TextBlock Text="{Binding DataContext.Translations.TermText, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</TabItem>
<TabItem Header="{Binding Translations.BuildingsText}" TabIndex="1" >
<DataGrid x:Name="ovTiBuildings"
ItemsSource="{Binding CodeBooks[Building], Mode=TwoWay}"
SelectedItem="{Binding SelectedBuilding, Mode=TwoWay}"
ColumnWidth="*" AutoGenerateColumns="False" HorizontalAlignment="Stretch"
Visibility="Visible" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}" Width="auto">
<DataGridTextColumn.Header>
<TextBlock Text="{Binding DataContext.Translations.NumberText, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=DescriptionTerm}" Width="auto">
<DataGridTextColumn.Header>
<TextBlock Text="{Binding DataContext.Translations.TermText, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</TabItem>
Can you help me how to correctly bind HeaderText?