如何设置TreeView节点的背景颜色?

时间:2018-06-13 11:24:35

标签: uwp

我们将根节点添加到TreeView控件,如下所示。但是如何设置该根节点的背景颜色。还要将bg-color设置为任何当前选定的项目? (我想,每个节点都是TreeViewItem,但我无法从节点中检索它

TreeViewNode rootNode1 = new TreeViewNode() {Content = "Flavors"};
sampleTreeView.RootNodes.Add(rootNode1);

2 个答案:

答案 0 :(得分:1)

TreeView中,每个TreeViewNode都是TreeViewItem,他们使用相同的TreeViewItemStyle。它没有公开从Node获取TreeViewItem的方法。但是你可以修改它的样式,使它看起来不同。

您可以通过以下步骤获取默认样式TreeView

在TreeView页面中,单击文档大纲选项卡,在文档大纲窗口中,您可以找到TreeView,然后可以为treeView创建默认样式如下图所示,

enter image description here

然后单击“确定”以创建复制样式,之后,您可以在Page.Resources中看到该样式。您可以更改资源中的样式以更改节点的样式和选择节点的背景。

这是TreeView的默认样式,

<Style x:Key="TreeViewStyle1" TargetType="TreeView">
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TreeView">
                <TreeViewList x:Name="ListControl" AllowDrop="True"
                              CanReorderItems="True" CanDragItems="True"
                              ItemContainerStyle="{StaticResource TreeViewItemStyle}"
                              ItemTemplate="{StaticResource TreeViewItemDataTemplate}">
                    <TreeViewList.ItemContainerTransitions>
                        <TransitionCollection>
                            <ContentThemeTransition/>
                            <ReorderThemeTransition/>
                            <EntranceThemeTransition IsStaggeringEnabled="False"/>
                        </TransitionCollection>
                    </TreeViewList.ItemContainerTransitions>
                </TreeViewList>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在这种风格中,您可以看到TreeViewList的ItemContainerStyle正在使用TreeViewItemStyle且其ItemTemplate正在使用TreeViewItemDataTemplate,您也可以在此页面的资源中找到它们从我们的上述步骤生成。

要更改节点的背景,您可以通过将网格背景设置为红色来修改TreeViewItemDataTemplate以下代码,

<DataTemplate x:Key="TreeViewItemDataTemplate">
    <Grid Height="44" Background="Red">
        <TextBlock HorizontalAlignment="Left"
                   Style="{ThemeResource BodyTextBlockStyle}" 
                   Text="{Binding Content}" VerticalAlignment="Center"/>
    </Grid>
</DataTemplate>

要将bg-color设置为任何当前选定的项目,您可以修改TreeViewItemStyle选定的VisualState,

...
    <VisualState x:Name="Selected">
        <VisualState.Setters>
            <Setter Target="ContentPresenterGrid.Background" Value="Green"/>
            <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource TreeViewItemForegroundSelected}"/>
            <Setter Target="ContentPresenterGrid.BorderBrush" Value="{ThemeResource TreeViewItemBorderBrushSelected}"/>
        </VisualState.Setters>
    </VisualState>
...

此外,您还可以通过修改ContentPresenterGrid的背景来连续更改节点的背景。

 <Grid x:Name="ContentPresenterGrid" Background="Yellow" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Margin="0,0,0,0">

答案 1 :(得分:1)

更改所有项目背景颜色的最简单方法是像这样覆盖TreeViewItemBackground ThemeResource

<TreeView>
    <TreeView.Resources>
        <ResourceDictionary>
            <ResourceDictionary.ThemeDictionaries>
                <ResourceDictionary x:Key="Default">
                    <SolidColorBrush x:Key="TreeViewItemBackground">Red</SolidColorBrush>
                </ResourceDictionary>
            </ResourceDictionary.ThemeDictionaries>
        </ResourceDictionary>
    </TreeView.Resources>
</TreeView>

如果要更改所选颜色,请对TreeViewItemBackgroundSelected资源执行相同操作。

更改根节点的背景颜色只需要花费更多的精力。您将必须提取默认样式和模板。完成此操作后,您可以处理Loaded的{​​{1}}中根Grid元素的TreeViewItem事件,并以编程方式检查ControlTemplate是否内容具有任何子节点。请参考以下示例标记。

TreeViewItem

...以及以下代码:

<TreeView x:Name="treeView">
    <TreeView.RootNodes>
        <TreeViewNode Content="Root A">
            <TreeViewNode.Children>
                <TreeViewNode Content="a" />
                <TreeViewNode Content="b" />
                <TreeViewNode Content="c" />
            </TreeViewNode.Children>
        </TreeViewNode>
        <TreeViewNode Content="Root B">
            <TreeViewNode.Children>
                <TreeViewNode Content="d" />
                <TreeViewNode Content="e" />
                <TreeViewNode Content="f" />
            </TreeViewNode.Children>
        </TreeViewNode>
    </TreeView.RootNodes>
    <TreeView.Resources>
        <Style x:Key="BaseTextBlockStyle" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="XamlAutoFontFamily"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="FontSize" Value="15"/>
            <Setter Property="TextTrimming" Value="None"/>
            <Setter Property="TextWrapping" Value="Wrap"/>
            <Setter Property="LineStackingStrategy" Value="MaxHeight"/>
            <Setter Property="TextLineBounds" Value="Full"/>
        </Style>
        <Style x:Key="BodyTextBlockStyle" BasedOn="{StaticResource BaseTextBlockStyle}" TargetType="TextBlock">
            <Setter Property="FontWeight" Value="Normal"/>
            <Setter Property="FontSize" Value="15"/>
        </Style>
        <Style x:Key="CaptionTextBlockStyle" BasedOn="{StaticResource BaseTextBlockStyle}" TargetType="TextBlock">
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="FontWeight" Value="Normal"/>
        </Style>
        <DataTemplate x:Key="TreeViewItemDataTemplate">
            <Grid Height="44">
                <TextBlock HorizontalAlignment="Left" Style="{ThemeResource BodyTextBlockStyle}" Text="{Binding Content}" VerticalAlignment="Center"/>
            </Grid>
        </DataTemplate>
    </TreeView.Resources>
    <TreeView.Template>
        <ControlTemplate TargetType="TreeView">
            <TreeViewList x:Name="ListControl" AllowDrop="True" CanReorderItems="True" CanDragItems="True" 
                                  ItemTemplate="{StaticResource TreeViewItemDataTemplate}">
                <TreeViewList.ItemContainerStyle>
                    <Style TargetType="TreeViewItem">
                        <Setter Property="Padding" Value="0"/>
                        <Setter Property="Background" Value="{ThemeResource TreeViewItemBackground}"/>
                        <Setter Property="BorderBrush" Value="{ThemeResource TreeViewItemBorderBrush}"/>
                        <Setter Property="BorderThickness" Value="{ThemeResource TreeViewItemBorderThemeThickness}"/>
                        <Setter Property="GlyphBrush" Value="{ThemeResource TreeViewItemForeground}"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="TreeViewItem">
                                    <Grid x:Name="ContentPresenterGrid" 
                                                  Background="{TemplateBinding Background}" 
                                                  BorderThickness="{TemplateBinding BorderThickness}" 
                                                  BorderBrush="{TemplateBinding BorderBrush}" 
                                                  Margin="0,0,0,0"
                                                  Loaded="ContentPresenterGrid_Loaded">
                                        <VisualStateManager.VisualStateGroups>
                                            ...
                                        </VisualStateManager.VisualStateGroups>
                                        <Grid x:Name="MultiSelectGrid">
                                            <Grid.Resources>
                                                <Style x:Name="TreeViewMultiSelectCheckBox" TargetType="CheckBox">
                                                    <Setter Property="Background" Value="{ThemeResource CheckBoxBackgroundUnchecked}"/>
                                                    <Setter Property="Foreground" Value="{ThemeResource CheckBoxForegroundUnchecked}"/>
                                                    <Setter Property="BorderBrush" Value="{ThemeResource CheckBoxBorderBrushUnchecked}"/>
                                                    <Setter Property="HorizontalAlignment" Value="Left"/>
                                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                                    <Setter Property="HorizontalContentAlignment" Value="Left"/>
                                                    <Setter Property="VerticalContentAlignment" Value="Top"/>
                                                    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
                                                    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
                                                    <Setter Property="MinWidth" Value="32"/>
                                                    <Setter Property="MinHeight" Value="32"/>
                                                    <Setter Property="Template">
                                                        <Setter.Value>
                                                            <ControlTemplate TargetType="CheckBox">
                                                                <Grid x:Name="RootGrid" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Width="32">
                                                                    <VisualStateManager.VisualStateGroups>
                                                                        <VisualStateGroup x:Name="CombinedStates">
                                                                            <VisualState x:Name="UncheckedNormal">
                                                                                <Storyboard>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Foreground">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxCheckGlyphForegroundUnchecked}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                </Storyboard>
                                                                            </VisualState>
                                                                            <VisualState x:Name="UncheckedPointerOver">
                                                                                <Storyboard>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Foreground">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxCheckGlyphForegroundUncheckedPointerOver}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                </Storyboard>
                                                                            </VisualState>
                                                                            <VisualState x:Name="UncheckedPressed">
                                                                                <Storyboard>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Foreground">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxCheckGlyphForegroundUncheckedPressed}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                </Storyboard>
                                                                            </VisualState>
                                                                            <VisualState x:Name="UncheckedDisabled">
                                                                                <Storyboard>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Foreground">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxCheckGlyphForegroundUncheckedDisabled}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                </Storyboard>
                                                                            </VisualState>
                                                                            <VisualState x:Name="CheckedNormal">
                                                                                <Storyboard>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalRectangle" Storyboard.TargetProperty="Fill">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TreeViewItemCheckBoxBackgroundSelected}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalRectangle" Storyboard.TargetProperty="Stroke">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TreeViewItemCheckBoxBorderSelected}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Foreground">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TreeViewItemCheckGlyphSelected}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                    <DoubleAnimation Duration="0" Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Opacity" To="1"/>
                                                                                </Storyboard>
                                                                            </VisualState>
                                                                            <VisualState x:Name="CheckedPointerOver">
                                                                                <Storyboard>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalRectangle" Storyboard.TargetProperty="Fill">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TreeViewItemCheckBoxBackgroundSelected}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalRectangle" Storyboard.TargetProperty="Stroke">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TreeViewItemCheckBoxBorderSelected}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Foreground">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TreeViewItemCheckGlyphSelected}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                    <DoubleAnimation Duration="0" Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Opacity" To="1"/>
                                                                                </Storyboard>
                                                                            </VisualState>
                                                                            <VisualState x:Name="CheckedPressed">
                                                                                <Storyboard>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalRectangle" Storyboard.TargetProperty="Fill">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TreeViewItemCheckBoxBackgroundSelected}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalRectangle" Storyboard.TargetProperty="Stroke">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TreeViewItemCheckBoxBorderSelected}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Foreground">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TreeViewItemCheckGlyphSelected}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                    <DoubleAnimation Duration="0" Storyboard.TargetName="NormalRectangle" Storyboard.TargetProperty="StrokeThickness" To="{ThemeResource CheckBoxCheckedStrokeThickness}"/>
                                                                                    <DoubleAnimation Duration="0" Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Opacity" To="1"/>
                                                                                </Storyboard>
                                                                            </VisualState>
                                                                            <VisualState x:Name="CheckedDisabled">
                                                                                <Storyboard>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalRectangle" Storyboard.TargetProperty="Fill">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TreeViewItemCheckBoxBackgroundSelected}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalRectangle" Storyboard.TargetProperty="Stroke">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TreeViewItemCheckBoxBorderSelected}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Foreground">
                                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TreeViewItemCheckGlyphSelected}"/>
                                                                                    </ObjectAnimationUsingKeyFrames>
                                                                                    <DoubleAnimation Duration="0" Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Opacity" To="1"/>
                                                                                </Storyboard>
                                                                            </VisualState>
                                                                        </VisualStateGroup>
                                                                    </VisualStateManager.VisualStateGroups>
                                                                    <Grid Height="32" VerticalAlignment="Stretch">
                                                                        <Rectangle x:Name="NormalRectangle" Fill="{ThemeResource CheckBoxCheckBackgroundFillUnchecked}" HorizontalAlignment="Center" Height="20" StrokeThickness="{ThemeResource CheckBoxBorderThemeThickness}" Stroke="{ThemeResource CheckBoxCheckBackgroundStrokeUnchecked}" UseLayoutRounding="False" VerticalAlignment="Center" Width="20"/>
                                                                        <FontIcon x:Name="CheckGlyph" FontFamily="{ThemeResource SymbolThemeFontFamily}" Foreground="{ThemeResource CheckBoxCheckGlyphForegroundUnchecked}" FontSize="20" Glyph="&#xE001;" Opacity="0"/>
                                                                    </Grid>
                                                                </Grid>
                                                            </ControlTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </Style>
                                            </Grid.Resources>
                                            <StackPanel HorizontalAlignment="Stretch" Margin="{Binding TreeViewItemTemplateSettings.Indentation, RelativeSource={RelativeSource Mode=TemplatedParent}}" Orientation="Horizontal" VerticalAlignment="Stretch">
                                                <Grid>
                                                    <CheckBox x:Name="MultiSelectCheckBox" IsTabStop="False" Style="{StaticResource TreeViewMultiSelectCheckBox}" VerticalAlignment="Stretch" Visibility="Collapsed" Width="32"/>
                                                    <Border x:Name="MultiArrangeOverlayTextBorder" Background="{ThemeResource SystemControlBackgroundAccentBrush}" BorderThickness="2" BorderBrush="{ThemeResource SystemControlBackgroundChromeWhiteBrush}" HorizontalAlignment="Center" Height="20" IsHitTestVisible="False" MinWidth="20" Opacity="0" VerticalAlignment="Center">
                                                        <TextBlock x:Name="MultiArrangeOverlayText" AutomationProperties.AccessibilityView="Raw" HorizontalAlignment="Center" IsHitTestVisible="False" Style="{ThemeResource CaptionTextBlockStyle}" Text="{Binding TreeViewItemTemplateSettings.DragItemsCount, RelativeSource={RelativeSource Mode=TemplatedParent}}" VerticalAlignment="Center"/>
                                                    </Border>
                                                </Grid>
                                                <Grid x:Name="ExpandCollapseChevron" Background="Transparent" Opacity="{TemplateBinding GlyphOpacity}" Padding="12,0,12,0" Width="Auto">
                                                    <TextBlock FontFamily="{StaticResource SymbolThemeFontFamily}" Foreground="{TemplateBinding GlyphBrush}" FontSize="{TemplateBinding GlyphSize}" Height="12" Text="{TemplateBinding CollapsedGlyph}" VerticalAlignment="Center" Visibility="{Binding TreeViewItemTemplateSettings.CollapsedGlyphVisibility, RelativeSource={RelativeSource Mode=TemplatedParent}}" Width="12"/>
                                                    <TextBlock FontFamily="{StaticResource SymbolThemeFontFamily}" Foreground="{TemplateBinding GlyphBrush}" FontSize="{TemplateBinding GlyphSize}" Height="12" Text="{TemplateBinding ExpandedGlyph}" VerticalAlignment="Center" Visibility="{Binding TreeViewItemTemplateSettings.ExpandedGlyphVisibility, RelativeSource={RelativeSource Mode=TemplatedParent}}" Width="12"/>
                                                </Grid>
                                                <ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentTransitions="{TemplateBinding ContentTransitions}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                            </StackPanel>
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TreeViewList.ItemContainerStyle>
                <TreeViewList.ItemContainerTransitions>
                    <TransitionCollection>
                        <ContentThemeTransition/>
                        <ReorderThemeTransition/>
                        <EntranceThemeTransition IsStaggeringEnabled="False"/>
                    </TransitionCollection>
                </TreeViewList.ItemContainerTransitions>
            </TreeViewList>
        </ControlTemplate>
    </TreeView.Template>
</TreeView>

它会产生以下结果:

enter image description here