WPF Binding to parent ItemsControl from inside of child ItemsControl data template

saving the parent item in the tag of the child itemscontrol could work

    <DataTemplate>

            <ItemsControl ItemsSource="{Binding Path=MySubCollection}" Tag="{Binding .}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Tag.Value, RelativeSource={RelativeSource  AncestorType={x:Type ItemsControl}}}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

    </DataTemplate>

its not tested but give you a hint in the right direction :)


Binding for Tag, as suggested in the other answer, is not required. All data can be obtained from DataContext of ItemControl (and this markup Tag="{Binding}" just copies DataContext into Tag property, which is redundant).

<ItemsControl ItemsSource="{Binding Path=MyParentCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding Path=MySubCollection}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=DataContext.Value, RelativeSource={RelativeSource AncestorType=ItemsControl}}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Tags:

C#

.Net

Wpf

Binding