在Treeview WPF中更改特定元素的颜色

本文关键字:元素 颜色 Treeview WPF | 更新日期: 2023-09-27 18:15:42

我的WPF应用程序中有一个Treeview。在运行时,如果Tree的元素满足一定条件,则应将其Font color由黑变为红。

XAML

<TreeView Grid.Column="0" Grid.Row="0"  HorizontalAlignment="Stretch" Name="treeView1" 
                      VerticalAlignment="Stretch"
                      SelectedItemChanged="treeView1_SelectedItemChanged" HorizontalContentAlignment="Stretch" 
                      VerticalContentAlignment="Top" BorderThickness="0,0,0,1" BorderBrush="LightGray">
    <TreeViewItem Header="Head Tree" ItemsSource="{Binding MainComps}">
        <TreeViewItem.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
            <Setter Property="FontWeight" Value="Normal" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="FontWeight" Value="Bold" />
                </Trigger>
                <DataTrigger Binding="{Binding IsSelected}" Value="True">
                        <Setter Property="Foreground" Value="RED" />
                </DataTrigger>
            </Style.Triggers>                                 
        </Style>
    </TreeViewItem.ItemContainerStyle>
    <TreeViewItem.Resources>
        <HierarchicalDataTemplate  DataType="{x:Type TextBlock}" ItemsSource="{Binding Children}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Head Tree" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type local:MainCompViewModel}" ItemsSource="{Binding Children}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Maincompname}" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type local:FeatureViewModel}" ItemsSource="{Binding Children}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding FeatureName}" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type local:CompViewModel}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Component}" />
            </StackPanel>
        </DataTemplate>                               
    </TreeViewItem.Resources>
    </TreeViewItem>
</TreeView>

Code behind

private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    if(selected Item meets certain condition)
    {
         //Change color of tree node
    }
}

我如何改变特定节点的颜色并使其保持相同的颜色,以便在再次展开时它应该在RED中。

在Treeview WPF中更改特定元素的颜色

您可以在模型中创建一个布尔属性,当元素满足条件时该属性为真。然后像这样绑定前台:

                       <TreeView.ItemContainerStyle>
                            <Style TargetType="{x:Type TreeViewItem}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=BoolProp}" Value="False">
                                        <Setter Property="Foreground" Value="Blue"></Setter>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=BoolProp}" Value="True">
                                        <Setter Property="Foreground" Value="Red"></Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TreeView.ItemContainerStyle>

或带转换器:

                       <TreeView.ItemContainerStyle>
                            <Style TargetType="{x:Type TreeViewItem}">
                                <Setter Property="Foreground" Value="{Binding Path=BoolProp, Converter={StaticResource ResourceKey=TheKey}}"/>
                            </Style>
                        </TreeView.ItemContainerStyle>

只要改变前景:

TreeViewItem ti = (TreeViewItem)treeView1.SelectedItem;
ti.Foreground = Brushes.Red;

嵌入到模板中。您只能通过复制控件的默认Aero-Style并更改硬编码值来更改颜色。

或者通过钻取on-load可视化树来改变它。

获取默认样式&模板通过这个MSDN