7

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. 任何帮助,将不胜感激。

4

3 回答 3

8

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

                       <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>
于 2013-05-27T06:21:02.577 回答
4

只需更改前景:

TreeViewItem ti = (TreeViewItem)treeView1.SelectedItem;
ti.Foreground = Brushes.Red;
于 2013-05-27T06:15:05.107 回答
0

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

或者通过在加载时向下钻取可视化树来改变它。

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

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

于 2013-05-27T06:15:09.773 回答