I want to bind an nested DataGrid (RowDetailsTemplate) to different data-object with resp. to the data-object for the outer DataGrid.
It works properly for the outer object "Outer", but how can I access the inner data object "Inner".
The xaml:
<Window.Resources>
<local:MainWindowModel x:Key="vm"/>
</Window.Resources>
<Grid Name="grdMain" Height="170" VerticalAlignment="Top" DataContext="{Binding Source={StaticResource vm}}">
<DataGrid x:Name="datagridOuter"
ItemsSource="{Binding Outer}"
AutoGenerateColumns="False"
Height="170">
<DataGrid.Columns>
<DataGridTextColumn Header="Outer"
Binding="{Binding Path=Name}"
MinWidth="150"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid Name="datagridInner"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=DataContext.Inner}"
Height="50"
AutoGenerateColumns="False"
IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Inner"
Binding="{Binding Path=Name}"
MinWidth="150"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
The Item class:
public class Item
{
public string Name { get; set; }
public Item ( string name_ )
{
this.Name = name_;
}
} //eoClass
The MainWindowModel class:
class MainWindowModel : Notifier
{
public ObservableCollection<Item> outer;
public ObservableCollection<Item> Outer
{
get { return this.outer; }
set
{
this.outer = value;
OnPropertyChanged ( "Outer" );
}
}
public ObservableCollection<Item> inner;
public ObservableCollection<Item> Inner
{
get { return this.inner; }
set
{
this.inner = value;
OnPropertyChanged ( "Inner" );
}
}
} //eoClass

ItemsSource="{Binding ElementName=grdMain, Path=DataContext.Inner}"