【问题标题】:Highlight TextBlock only in TreeViewItem with Image仅在带有图像的 TreeViewItem 中突出显示 TextBlock
【发布时间】:2009-11-09 20:10:27
【问题描述】:

所以我有一个具有以下样式的 TreeViewItem:

                   <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="HeaderTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Image Name="img" Width="20" Height="16" Stretch="Uniform" Source="Images/Folder.png"/>
                                        <TextBlock Text="{Binding}" Margin="5,0" />
                                    </StackPanel>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

选中后,TextBlock AND Image 将突出显示。我试图突出显示 TextBlock,使其功能类似于文件资源管理器中的文件夹树。

【问题讨论】:

    标签: c# wpf treeview treeviewitem


    【解决方案1】:

    我找到了一个(有点老套)但轻量级的解决方案来解决这个问题。我看到这个问题已经很老了,但是,我会在这里发布解决方案供其他人查找。

    在我的 TreeView 中,当 TreeViewItem 的选择发生变化时,我覆盖了用于设置 TreeViewItem 背景的两个画笔。我还创建了画笔的副本,以便稍后在我的数据模板中恢复它们:

    <TreeView ItemsSource="{Binding Path=SomeDataSource}">
        <TreeView.Resources>
             <SolidColorBrush x:Key="_CustomHighlightBrushKey" Color="{Binding Source={StaticResource {x:Static SystemColors.HighlightBrushKey}}, Path=Color}" />
             <SolidColorBrush x:Key="_CustomControlBrushKey" Color="{Binding Source={StaticResource {x:Static SystemColors.ControlBrushKey}}, Path=Color}" />
             <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
             <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
        </TreeView.Resources>
    </TreeView>
    

    请注意,我无法将其与 DynamicResource 绑定一起使用,因此此解决方案可能不适用于主题更改。我很想知道是否有办法做到这一点。

    然后我使用以下分层数据模板来格式化树节点:

    <HierarchicalDataTemplate DataType="{x:Type SomeViewModelType}" ItemsSource="{Binding Path=Children}">
        <StackPanel Orientation="Horizontal">
            <StackPanel.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding Source={StaticResource {x:Static SystemColors.HighlightBrushKey}}, Path=Color}" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{Binding Source={StaticResource {x:Static SystemColors.ControlBrushKey}}, Path=Color}" />
            </StackPanel.Resources>
            <Image SnapsToDevicePixels="True" Source="...">
            </Image>
            <TextBlock Text="{Binding Path=DisplayName}" Margin="5,0">
                <TextBlock.Resources>
                    <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=IsSelected}" Value="True">
                                <Setter Property="Background" Value="{DynamicResource _CustomHighlightBrushKey}" />
                            </DataTrigger>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=IsSelected}" Value="True" />
                                    <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=IsSelectionActive}" Value="False" />
                                </MultiDataTrigger.Conditions>
                                <Setter Property="Background" Value="{DynamicResource _CustomControlBrushKey}" />
                            </MultiDataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Resources>
            </TextBlock>
        </StackPanel>
    </HierarchicalDataTemplate>
    

    请注意,我将系统画笔恢复为其原始(静态)值,以便可以正确呈现上下文菜单。

    【讨论】:

      【解决方案2】:

      您需要向上滚动自己的高亮标记,因此不要让控件将整个面板背景涂成蓝色,而是在 TreeViewItem.IsSelected 为 True 时根据触发器设置自己的高亮格式。

      在您的情况下,这会将文本容器背景设置为蓝色(设置时通常为白色)并将图像容器背景设置为白色,同时将整个容器背景设置为白色。

      方法描述在这里:link text

      【讨论】:

      • 这是有道理的,但并不像您提供的示例那么简单。每次我尝试使用 TargetName 来处理 TextBlock 时,都会收到一条错误消息,指出无法访问它。
      • 不要采取这种错误的方式,但是当您指出一个与我正在尝试做的事情有点接近的示例时,它对 WPF 的新手没有多大帮助。无论我尝试什么,每次我返回 TextBlock 时都会收到“TargetName 属性无法在样式设置器上设置”错误。
      • 我认为您收到该错误的原因是因为样式设置器将始终在使用该样式的控件上设置属性,该样式可以通过 TargetName 属性重定向。如果您查看我发布的第一个示例,触发器不是样式的一部分,它只是控件模板的一部分,我认为这可能是您出错的地方。
      猜你喜欢
      • 2013-09-24
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 2012-06-26
      • 2011-05-12
      • 1970-01-01
      • 2023-01-24
      相关资源
      最近更新 更多