在分层TreeView中更改突出显示颜色

时间:2010-04-16 07:17:27

标签: .net wpf treeview highlight treeviewitem

我有一个带有HierarchicalDataTemplate的TreeView。

<HierarchicalDataTemplate x:Key="treeViewItemTemplate" ItemsSource="{Binding GetChildren}">
    <DockPanel Margin="0,8,8,0">
        <Image Source="{Binding GetImage}" Width="16" Height="16" />
        <local:MonitorTriStateCheckBox Margin="4,0,0,0" IsChecked="{Binding IsChecked}" Click="CheckBox_Clicked" Tag="{Binding UniqueKey}" Style="{DynamicResource CheckBox}"></local:MonitorTriStateCheckBox>
        <TextBlock Margin="4,0,0,0" Text="{Binding Name}" Style="{DynamicResource TextBlock}">
        </TextBlock>
    </DockPanel>
    <HierarchicalDataTemplate.Triggers>
        <Trigger Property="TreeViewItem.IsSelected" Value="True">
            <Setter Property="TreeViewItem.Background" Value="Orange" />
        </Trigger>
    </HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>

正如您在代码中看到的,我设置了TreeViewItem的选择触发器,但这没有任何效果。我试过这个:

<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
        <Setter Property="Visibility" Value="{Binding IsVisible, Mode=TwoWay}" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Orange" />
            </Trigger>
        </Style.Triggers>
    </Style>
</TreeView.ItemContainerStyle>

但这也没有效果。

有没有人知道该怎么做,更改TreeViewItem的高亮颜色?

1 个答案:

答案 0 :(得分:9)

试试这个......

<TreeView.Resources>                                                
    <SolidColorBrush Color="Green" x:Key="{x:Static SystemColors.HighlightBrushKey}"/>
</TreeView.Resources>