Question

In the following code I am trying to set the style of the first item in my collection to one template and the rest to a different template by checking if the PreviousElement is null. I think my relativesource is incorrect because the trigger condition is always true. What should the path be?

    <DataTemplate x:Key="RowItemTemplate">
        <ContentPresenter Content="{Binding}">
            <ContentPresenter.Style>
                <Style TargetType="{x:Type ContentPresenter}">
                    <Setter Property="ContentTemplate" Value="{StaticResource ComparisonTemplate}"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">
                            <Setter Property="ContentTemplate" Value="{StaticResource SourceTemplate}"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentPresenter.Style>
        </ContentPresenter>
    </DataTemplate>
Was it helpful?

Solution

I created an interface that has a bool to determine which template a particular element should use:

    <DataTemplate x:Key="RowItemTemplate">
        <ContentPresenter Content="{Binding}">
            <ContentPresenter.Style>
                <Style TargetType="{x:Type ContentPresenter}">
                    <Setter Property="ContentTemplate" Value="{StaticResource ComparisonTemplate}"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding DataItem}" Value="true">
                            <Setter Property="ContentTemplate" Value="{StaticResource SourceTemplate}"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentPresenter.Style>
        </ContentPresenter>
    </DataTemplate>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top