如何为 treeview 项目和 parent 制作相同的颜色?
How to make same color for treeview item and parent?
我正在 wpf 中创建 Treeview。
每个 parent 项目使用转换器设置随机前景色。
我希望它的所有 children 设置相同的颜色。仅使用 xaml.
这是我的部分代码:
<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>
我想做的是将 HierarchicalData 中的第二个树视图项(在 xaml 代码中)Children 设置为与其 parent 相同的颜色。
有办法吗?
提前致谢,
您可以尝试使用FindAncestor
RelativeSource 模式。
只需用这个替换您的 Note
模板:
<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>
我使用了 DataTemplate,但如果您愿意,可以继续使用 HierarchicalDataTemplate。
我正在 wpf 中创建 Treeview。
每个 parent 项目使用转换器设置随机前景色。 我希望它的所有 children 设置相同的颜色。仅使用 xaml.
这是我的部分代码:
<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>
我想做的是将 HierarchicalData 中的第二个树视图项(在 xaml 代码中)Children 设置为与其 parent 相同的颜色。 有办法吗?
提前致谢,
您可以尝试使用FindAncestor
RelativeSource 模式。
只需用这个替换您的 Note
模板:
<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>
我使用了 DataTemplate,但如果您愿意,可以继续使用 HierarchicalDataTemplate。