我有 2 嵌套DataContexts
在以下代码(DataContext
和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>
我想分配myViewModel
(回)给ComboBox's
DataContext
. 问题是ComboBox
已经“覆盖”DataContext
了:
<DataGrid ItemsSource="{Binding muObservableCollection}" ... >
我该怎么做?