Search code examples
xamlsettercontentcontrol

ContentControl - Content setter inside ItemsControl


I have a weird Behaviour in my ContentControl and don't see why it behaves this way.

This Xaml code list the items of my ObservableCollection

<ItemsControl ItemsSource="{Binding Stops}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ContentControl>

                <local:TripDetailListItemControl />

            </ContentControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

But this only displays the First item of the List

<ItemsControl ItemsSource="{Binding Stops}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ContentControl>
                <ContentControl.Style>
                    <Style TargetType="{x:Type ContentControl}">
                        <Setter Property="Content">
                            <Setter.Value>

                                <local:TripDetailListItemControl/>

                            </Setter.Value>
                        </Setter>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

What is the Difference? Am I missing an Enumarator with the Style override?

The Background, why I need it this way, is that I have a Proeprty in this TripDetailListItemControl that changes the view of this Item. So I want a DataTrigger in this Style to Display it a different way.

But the list is not shown in the first place. What to I need to add in the Content Setter to display all Items?


Solution

  • add BasedOn:

    <Style TargetType="{x:Type ContentControl}" BasedOn="{StaticResource {x:Type ContentControl}}">
    

    or:

    <Style TargetType="{x:Type ContentControl}">
        <Setter Property="Content">
            <Setter.Value>
                <ContentPresenter></ContentPresenter>
            </Setter.Value>
         </Setter>
         <Setter Property="ContentTemplate">
             <Setter.Value>
                 <DataTemplate>
                     <local:TripDetailListItemControl/>
                 </DataTemplate>
             </Setter.Value>
         </Setter>
    </Style>