如何在不影响子控件的情况下对控件应用属性

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后出现的灰色区域:

enter image description here

这是另一个示例:

enter image description here


我假设您需要为您的 TreeView 设计样式,而不是为整个 TreeView 设置 HighlightBrushKey 的范围... - Spontifixus
能否请 @Spontifixus 详细解释一下? - Dot NET
1个回答

4
为了获得所需的行为,您需要为 TreeViewItem 提供一种新的默认样式和模板。在该模板内部,您可以更改突出显示项目的背景颜色,而不影响 TreeViewItem 的所有子项的背景。
您可以在 MSDN 中找到包含模板的样式示例:TreeViewItem ControlTemplate Example
第一步:将样式导入应用程序
您需要使样式和模板可用于您的 TreeView。因此,请复制网站上的 XAML 并将其粘贴到您的 TreeView 的资源部分中:
<TreeView x:Name="tvFilters" ...>
    <TreeView.Resources>

        <!-- paste copied styles here -->

    </TreeView.Resources>
</TreeView>

注意: 确保您还复制了所提供示例底部命名为GlyphBrushSolidColorBrush。否则,您的代码将无法正常工作。

第二步: 修改代码以适应您的需求

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

  1. Remove x:Key="{x:Type TreeViewItem}" from the following line

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

    so that it looks like

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

    That will apply the style to all items in the TreeView

  2. In the style for the TreeViewItem find the <Trigger Property="IsSelected" Value="true"> and replace

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

    with

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

    NOTE: Both values (Foreground and Background) are being replaced!

  3. In the style for the TreeViewItem find the <MultiTrigger> that has a <Condition Property="IsSelected" Value="true"/> and replace

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

    with

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

结果

在进行修改之前,TreeView将会呈现如下状态:

enter image description here

在进行修改之后,TreeView上的蓝色高亮将消失,但是在ComboBox中仍然可用:

enter image description here


再次问候,实际上我注意到发生了一些奇怪的事情 - 我希望应该很容易解决,并且源自模板。由于某种原因,在单击TreeView中的元素后,该元素的部分背景会变成灰色。我已经在问题中添加了截图以反映这一点。 - Dot NET
每个TreeViewItem都有一些文本框、组合框和标签。 - Dot NET
似乎其中一个是有责任的。我的例子中只是简单包含一个没有特殊样式的“ComboBox”。 - Spontifixus
2
你需要调整MultiTrigger的背景,将IsSelect = true设置为正确的触发器,并且所有内容都会正常 :) - Maverik
感谢@Maverik和Spontifixus! - Dot NET
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接