Ofek Agmon
Ofek Agmon

Reputation: 5198

How to make same color for treeview item and parent?

I am creating Treeview in wpf.

Each parent item set random foreground color using converter. I want all its children to set the same color. using only xaml.

Here is part of my code:

<HierarchicalDataTemplate DataType="{x:Type sotc:Category}" 
ItemsSource="{Binding Path=NoteList}">
  <TextBlock Text="{Binding Path=Name}" Forground="{Binding Path=Name,  Convert={StaticResource Converter1}}"/>
  </HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type sotc:Note}">
  <TextBlock Text="{Binding Path=Name}" />
  </HierarchicalDataTemplate>

What I wish to do is to set the second treeview item (in the xaml code), Children in HierarchicalData the same forgound color as its parent. Is there a way to do that?

Thank Ahead,

Upvotes: 1

Views: 342

Answers (1)

Il Vic
Il Vic

Reputation: 5666

You can try by using the FindAncestor RelativeSource mode. Just replace your Note template with this one:

<DataTemplate DataType="{x:Type sotc:Note}">
    <TextBlock Text="{Binding Path=Name}"
               Foreground="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=2, AncestorType=TreeViewItem}, Path=Header.Name, Converter={StaticResource Converter1}}"/>
</DataTemplate>

I used a DataTemplate, but you can keep using a HierarchicalDataTemplate if you prefer.

Upvotes: 1

Related Questions