Steve
Steve

Reputation: 12004

WPF color nodes based on view property

I have a WPF treeview that I would like the color of a node to be based on a particular getter. I can't figure how to databind for that case. I would like it to look like this except that odd numbers would be a child node of the even numbers

Upvotes: 2

Views: 852

Answers (1)

svick
svick

Reputation: 244918

If you already use HierarchicalDataTemplate, you can simply add a trigger:

<TreeView ItemsSource="{Binding}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Name}">
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Highlight}" Value="True">
                                <Setter Property="Background" Value="Yellow" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

Upvotes: 5

Related Questions