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

时间:2013-05-27 06:09:30

标签: c# wpf treeview

我的Treeview申请表中有WPF。在运行时,在运行时,如果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。 任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:4)

只需更改前景:

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

答案 2 :(得分:0)

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

或者通过在负载下钻取可视树来改变它。

获取默认样式&amp; tenmplate通过这个MSDN

也可以从这里检查步骤EXAMPLE