I have 2 nested DataContexts in following code (DataContext and ItemsSource):
<TabItem Header="Something" Name="myTabItemName" d:DataContext="{Binding myViewModel}">
<Grid>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding muObservableCollection}" Name="myDataGridName" HeadersVisibility="Column">
<DataGrid.Columns>
<DataGridTextColumn Header="Foo 01" Binding="{Binding Foo_01}" />
<DataGridTextColumn Header="Foo 02" Binding="{Binding Foo_02}" />
<DataGridTemplateColumn Header="My custom combobox">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<!-- Here I want to bind back to 'myViewModel's' myProperty. I've tried this way: -->
<ComboBox ItemsSource="{Binding Path=myProperty, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</TabItem>
I want to assign myViewModel (back) to ComboBox's DataContext. The problem is that ComboBox has ‘overwritten’ DataContext in line:
<DataGrid ItemsSource="{Binding muObservableCollection}" ... >
How can I do it?
Should do the trick. However I would like to point out that what you want does not make sense: you bind your TabItem let’s say to a Order-instance, the DataGrid to its Orderlines, why do you want a combobox from the Order in the DataGrid? Al the columns will display the same value and if you change one row, the values for all rows change.
I have run into this situation myself a couple of times that I needed access to an higher level (due to refactoring in combination with commands), but it is rather rare.