开发者

Set ItemsSource to ContentPresenter.Content

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.Resourc开发者_如何学JAVAes>
<ItemsControl Name="comparisonGrid" ItemTemplate="{StaticResource ResourceKey=RowItemTemplate}">
</ItemsControl>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜