2
votes

I have a custom control as follows:

<CustomControl>
    <CustomControl.ContentTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>                    
        </DataTemplate>
    </CustomControl.ContentTemplate>
</CustomControl>

In the control template of the CustomControl, I try to bind to the CustomControl.ContentTemplate from within a DataTemplate, but it does not work:

<ListBox
ItemsSource="{Binding SearchResultsList}">
<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Vertical">
            <ContentControl
                Content="{Binding}"
                ContentTemplate="{TemplateBinding ContentTemplate}">
            </ContentControl>
            <ItemsControl
                ItemsSource="{Binding HierarchyPath}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="->"/>
                            <TextBlock Text="{Binding Name}"/>
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Note: ContentTemplate="{TemplateBinding ContentTemplate}"

I know that you cannot use TemplateBinding inside a Datatemplate, even though the DataTemplate is inside a control template. But does anyone know how to achieve what I want to achieve without using TemplateBinding?

1

1 Answers

0
votes

Your best bet is to use the ListBox.ItemContainerStyle, and use it to make a ControlTemplate.

Something like this:

<ListBox>
<ListBox.ItemContainerStyle>
    <Style  TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <StackPanel Orientation="Vertical">
                        <ContentControl
                            Content="{Binding}"
                            ContentTemplate="{TemplateBinding ContentTemplate}">
                        </ContentControl>
                        <ItemsControl
                            ItemsSource="{Binding HierarchyPath}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="->"/>
                                        <TextBlock Text="{Binding Name}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ListBox.ItemContainerStyle>
</ListBox>