Question

I have tried this:

<DataTemplate x:Key="RowItemTemplate">
    <ItemsControl ItemTemplate="{StaticResource ResourceKey=BorderItemTemplate}" ItemsSource="ContentPresenter.Content">
    </ItemsControl>
</DataTemplate>

and it results in a stack overflow. How to I set the ItemsSource of an ItemsControl to the content of the ContentPresenter?

Edit:

Changed ItemsSource="ContentPresenter.Content" to just ItemsSource="{Binding}" but I'm still getting a stack overflow. The ItemsSource of the main ItemsControl is set to new List<List<string>> { new List<string> { "1", "2", "3", "4" }, new List<string> { "1", "2", "3" }, new List<string> { "1", "2" }, new List<string> { "1" } }; Here is a larger piece of my code:

<UserControl.Resources>
    <DataTemplate x:Key="BorderItemTemplate">
        <Border RenderTransformOrigin="0.5,0.5">
            <Border.RenderTransform>
                <RotateTransform Angle="-135"/>
            </Border.RenderTransform>
            <ContentPresenter/>
        </Border>
    </DataTemplate>
    <DataTemplate x:Key="RowItemTemplate">
        <ItemsControl ItemTemplate="{StaticResource ResourceKey=BorderItemTemplate}" ItemsSource="{Binding}">
        </ItemsControl>
    </DataTemplate>
</UserControl.Resources>
<ItemsControl Name="comparisonGrid" ItemTemplate="{StaticResource ResourceKey=RowItemTemplate}">
</ItemsControl>
Was it helpful?

Solution

If you are just trying to set the ItemsSource property to the data you are currently templating, you can use the following:

<DataTemplate x:Key="RowItemTemplate">
    <ItemsControl ItemsSource="{Binding}">
    </ItemsControl>
</DataTemplate>

The binding is interpreted as "the value of this.DataContext", and DataContext is always set to the value that you are templating within a DataTemplate.

Edit

Looking at the OPs full code, I think that the problem is down to using the ContentPresenter within the BorderItemTemplate. Assuming that the aim is to display the number to which it is bound, it should be replaced by the following:

<DataTemplate x:Key="BorderItemTemplate">
    <Border RenderTransformOrigin="0.5,0.5">
        <Border.RenderTransform>
            <RotateTransform Angle="-135"/>
        </Border.RenderTransform>
        <TextBlock Text="{Binding}" />
    </Border>
</DataTemplate>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top