在DataTemplate和样式中定义不同的前景颜色

时间:2018-08-23 08:14:56

标签: c# wpf

对于TreeViewItem,我使用DataTemplate。在此模板中,我将前景颜色定义为蓝色。当我选择TreeviewItem时,我希望前景颜色为白色。我的第一个想法是创建样式,但是样式不起作用。显然我做错了。有想法吗?

   <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>

1 个答案:

答案 0 :(得分:1)

您正在将Foreground属性分配给特定的目标名称,因此,您还必须更改该特定的TargetName的前景。您可以在Datatemplate.Triggers内实现这一目标,如下所述。

<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>