Fluxkompensator
Fluxkompensator

Reputation: 9

Define diffent Foreground-Color in DataTemplate and Style

for a TreeViewItem i use a DataTemplate. In this Template i define the Foreground-Color as blue. When i selected a TreeviewItem i would like have the Foreground-Color in white. My first thought was to create a Style, but the style doesn`t work. Obviously i make some wrong. Any idea?

   <DataTemplate x:Key="styDriveTreeNode" DataType="{x:Type local:NodeDrive }">
        <Grid  Margin="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="16"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image x:Name="picDrv" Grid.Column="0" Grid.Row="0" Grid.RowSpan="1" Height="16" Width="16"/>
            <Label x:Name="lblDrvName" Content="{Binding HeaderLabel}" Grid.Column="1" Grid.Row="0"  FontSize="12" Padding="0" Margin="2"/>
            <Label x:Name="lblDrvDetails" Content="{Binding HeaderDetails}" Grid.Column="1" Grid.Row="1" FontSize="10" Padding="0" Margin="2"/>
        </Grid>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding IsSystemPartition}" Value="true">
                <Setter TargetName="picDrv" Property="Source" Value="/Images/DriveWin16.png"/>
                <Setter TargetName="lblDrvName" Property="Foreground"  Value="Blue"/>
                <Setter TargetName="lblDrvDetails" Property="Foreground"  Value="Blue"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsDrive}" Value="true">
                <Setter TargetName="picDrv" Property="Source" Value="{StaticResource ResourceKey=imgDrv}"/>
            </DataTrigger>
        </DataTemplate.Triggers>

    </DataTemplate>
    <Style TargetType="{x:Type TreeViewItem}">            
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Foreground" Value="White"/>
            </Trigger>
        </Style.Triggers>                
    </Style>

Upvotes: 1

Views: 49

Answers (1)

maulik kansara
maulik kansara

Reputation: 1107

You are assigning Foreground property to specific target name, therefore, you have to change the foreground of that specific TargetName as well. And you can achieve that within Datatemplate.Triggers as mentioned below.

<DataTemplate x:Key="styDriveTreeNode" DataType="{x:Type local:NodeDrive }">
        <Grid  Margin="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="16"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image x:Name="picDrv" Grid.Column="0" Grid.Row="0" Grid.RowSpan="1" Height="16" Width="16"/>
            <Label x:Name="lblDrvName" Content="{Binding HeaderLabel}" Grid.Column="1" Grid.Row="0"  FontSize="12" Padding="0" Margin="2"/>
            <Label x:Name="lblDrvDetails" Content="{Binding HeaderDetails}" Grid.Column="1" Grid.Row="1" FontSize="10" Padding="0" Margin="2"/>
        </Grid>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding IsSystemPartition}" Value="true">
                <Setter TargetName="picDrv" Property="Source" Value="/Images/DriveWin16.png"/>
                <Setter TargetName="lblDrvName" Property="Foreground"  Value="Blue"/>
                <Setter TargetName="lblDrvDetails" Property="Foreground"  Value="Blue"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsDrive}" Value="true">
                <Setter TargetName="picDrv" Property="Source" Value="{StaticResource ResourceKey=imgDrv}"/>
            </DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=IsSelected}" Value="True">
                            <Setter TargetName="lblDrvName" Property="Foreground" Value="White"/>
                            <Setter TargetName="lblDrvDetails" Property="Foreground" Value="White"/>
                        </DataTrigger>
        </DataTemplate.Triggers>

    </DataTemplate>

Upvotes: 1

Related Questions