3

我有一个 TreeView ,它的高亮显示如下所示:

<TreeView Name="tvFilters" Margin="0,10,0,10" Background="White" BorderBrush="White">
            <TreeView.Resources>
                <!-- Disables the blue highlighting when a TreeViewItem is clicked -->
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">
                    Transparent
                </SolidColorBrush> 
            </TreeView.Resources>
 </TreeView>

编辑:这是我的 TreeView 的一部分 - 请注意单击 TreeViewItem 后出现的灰色区域:

在此处输入图像描述

这是另一个:

在此处输入图像描述

4

1 回答 1

4

要获得所需的行为,您需要为TreeViewItem. 然后,您可以在此模板中更改突出显示项目的背景颜色,而不会影响TreeViewItem.

您可以在 MSDN 中找到包含模板的样式示例:TreeViewItem ControlTemplate Example

第一步:将样式放入您的应用程序

您需要使样式和模板可用于您的TreeView. 因此,从网站复制 XAML 并将其粘贴到您的资源部分TreeView

<TreeView x:Name="tvFilters" ...>
    <TreeView.Resources>

        <!-- paste copied styles here -->

    </TreeView.Resources>
</TreeView>

注意:确保您还复制了所提供示例底部​​的SolidColorBrush名称。GlyphBrush否则你的代码将无法工作。

第二步:修改代码,使其符合您的需求

要使代码按您的意愿工作,您需要进行一些修改。

  1. x:Key="{x:Type TreeViewItem}"从以下行中删除

    <Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
    

    所以它看起来像

    <Style TargetType="{x:Type TreeViewItem}">
    

    这会将样式应用于TreeView

  2. 在样式中TreeViewItem查找<Trigger Property="IsSelected" Value="true">和替换

    <Setter TargetName="Bd"
            Property="Background"
            Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
    <Setter Property="Foreground" 
            Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
    

    <Setter TargetName="Bd"
            Property="Background"
            Value="Transparent" />
    <Setter Property="Foreground" 
            Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    

    注意:两个值 (ForegroundBackground) 都被替换了!

  3. 在样式中TreeViewItem查找<MultiTrigger>具有<Condition Property="IsSelected" Value="true"/>并替换的

    <Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
    

    <Setter TargetName="Bd" Property="Background" Value="Transparent"/>                                      
    

结果

在进行修改之前,TreeView将如下所示:

在此处输入图像描述

进行修改后, 上的蓝色突出显示TreeView将消失,而在 上仍然可用ComboBox

在此处输入图像描述

于 2012-12-06T09:48:41.337 回答