wpfxamlgridviewtextblockmultitrigger

Trigger Style on One Column of GridView


My question is similar to WPF Grid - How to apply a style for just one column? but applies to a GridView inside of a ListView

I'm trying to apply a trigger that will change the color of text within a TextBlock only in a given column within the GridView. If I take out the line commented THIS each textblock in the row is affected.

<ListView>
    <ListView.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Foreground" Value="White" />
        </Style>
    </ListView.Resources>
    <ListView.View>
        <GridView x:Name="MyGridView" ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeaderStyleNew}">
            <GridViewColumn Width="90" Header="Name" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Width="90" Header="Option" DisplayMemberBinding="{Binding Option}"/>
            <GridViewColumn Width="90" Header="Dir" DisplayMemberBinding="{Binding Directory}"/>
            <GridViewColumn Width="90" Header="Size" DisplayMemberBinding="{Binding Size}"/>
            <GridViewColumn Width="90" Header="Status" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Status}">
                            <TextBlock.Style>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Foreground" Value="White" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Status}" Value="Overheating">
                                            <DataTrigger.EnterActions>
                                                <BeginStoryboard>
                                                    <Storyboard>
                                                        <ColorAnimation Storyboard.TargetProperty="Foreground.Color" From="White" To="Red" Duration="0:0:2" AutoReverse="True" RepeatBehavior="Forever" />
                                                    </Storyboard>
                                                </BeginStoryboard>
                                            </DataTrigger.EnterActions>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

What changes to that line would make it so that only the 5th column which contains the text block is affected by the multitrigger?

[EDIT 1] Added code for GridView

[EDIT 2] I modified the last GridViewColumn to give it its own style

[EDIT 3] Removed the DisplayMemberBinding and added a Setter property... it works :).

Updated source code to reflect correct answer.


Solution

  • The easiest thing to do would be moving the style to the column's template:

    <GridViewColumn Width="90" Header="Status">
        <GridViewColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Status}">
                    <TextBlock.Style>
                         <!-- Style here without additional condition -->