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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16767241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:52:29  来源:igfitidea点击:

Change the colour of the particular element in Treeview WPF

c#wpftreeview

提问by BinaryMee

I have a Treeviewin my WPFapplication. On the fly, in run time, If the element of the Treemeets certain condition, It should change its Font colorfrom Black To Red.!

Treeview我的WPF应用程序中有一个。在飞行中,在运行时,如果元素Tree满足一定条件,它应该Font color从黑色变为红色。!

XAML

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

Code behind

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

How can I change the color of particular Node and leave it in the same color SO that when expanded again It should be in RED. Any help would be appreciated.

如何更改特定节点的颜色并使其保持相同的颜色,以便再次展开时它应该在RED. 任何帮助,将不胜感激。

回答by Florian Gl

You could create a boolean property in the model which is true when the elements meets the condition. Then you bind the Foreground like so:

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

                       <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>

or with converter:

或使用转换器:

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

回答by MedMik

Just change the Foreground:

只需更改前景:

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

回答by Freelancer

That is embedded into the template. You can only change the color by copying the default Aero-Stylefor the control and changing the hard-coded value.

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

Or by drilling down the visual tree on-load to change it that way.

或者通过在加载时深入查看可视化树来改变它。

To get the default style & tenmplate go through this MSDN

要获得默认样式和模板,请通过此MSDN

Can also check step wise EXAMPLEfrom here.

也可以从这里检查逐步示例