Creating a trigger in a CellTemplate for a ListView? (confusion with Templates in general)

.net, c#, wpf, xaml

Solution

Try this:

<GridViewColumn Header="Position">
    <GridViewColumn.CellTemplate>
        <DataTemplate DataType="{x:Type ListViewItem}">
            <TextBlock Name="TextBlockName" Text="{Binding Path=PositionCode}"></TextBlock>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding PositionCode}" Value="QB">
                    <Setter TargetName="TextBlockName" Property="Foreground" Value="Blue" />
                </DataTrigger>
                <DataTrigger Binding="{Binding PositionCode}" Value="RB">
                    <Setter TargetName="TextBlockName" Property="Foreground" Value="Green" />
                </DataTrigger>
                <DataTrigger Binding="{Binding PositionCode}" Value="WR">
                    <Setter TargetName="TextBlockName" Property="Foreground" Value="Red" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

Problem

I'm starting to get a little confused as I delve further into WPF and I feel like this example will help in better understanding things. My requirement is this: I have a ListView that is using a binding to a collection of plain .NET objects, I want to do two things: 1) highlight the cell of a row in the ListView if the value is a certain value - I figure I can use the GridViewColumn.CellTemplate for this and create a DataTemplate with a DataTrigger, however I am becoming confused here - is the DataType for the DataTemplate supposed to be the ListViewItem or is it supposed to be the type of the underlying object itself? This is a general point of confusion for me in WPF ..not knowing when to type it to the underlying collection object (which I've seen in examples) vs the list-item type itself. Here is my first attempt: ``` <GridViewColumn Header="Position"> <GridViewColumn.CellTemplate> <DataTemplate DataType="{x:Type ListViewItem}"> <TextBlock Text="{Binding Path=PositionCode}"></TextBlock> <DataTemplate.Triggers> <DataTrigger Binding="{Binding PositionCode}" Value="QB"> <Setter Property="Foreground" Value="Blue" /> </DataTrigger> <DataTrigger Binding="{Binding PositionCode}" Value="RB"> <Setter Property="Foreground" Value="Green" /> </DataTrigger> <DataTrigger Binding="{Binding PositionCode}" Value="WR"> <Setter Property="Foreground" Value="Red" /> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> ``` However, this not surprisingly leads to the error message ``` Cannot find the Template Property 'Background' on the type 'System.Windows.Controls.ContentPresenter' ``` 2) similar to 1) I want to have a similar rule on another criteria I want to highlight the entire row, instead of just the cell based on a similar DataTrigger property but same time I want the cell highlighting to take precedence over the row highlighting. How would I do this and what template do I need to override to do this? I'm guessing it's the ListView.ItemTemplate but what would the data type be?

Original source