改变WPF树视图方向的最佳方法是什么?我想使用expand-collapse功能从左到右而不是从上到下工作。即当我点击treenode的展开按钮时,我的子节点将显示在父节点右侧,缩进应该自上而下。此外,连接节点的垂直线现在必须是水平的。
答案 0 :(得分:6)
这是Josh Smith on CodeProject一篇很棒的文章,详细说明了如何做这种事情。
答案 1 :(得分:2)
要展开John Smith's CodeProject article,如果您希望仅在树中的特定级别(而不是像他的文章所示的所有级别)上进行水平布局,那么只需设置ItemsPanel
属性您希望获得TreeViewItem
级别的StackPanel
。
起初对我来说并不直观,但您可以通过ItemContainerStyle
的{{1}}属性访问此属性,以获取您想要水平的图层上方的图层。< / p>
像这样:
HierarchicalDataTemplate
遵循此模式,您可以为树中的任何单个图层设置水平布局,但根层除外。如果您希望根图层是水平的,那么只需在<ItemsPanelTemplate
x:Key="ItemsPanelForHorizontalItems">
<StackPanel
Orientation="Horizontal"/>
</ItemsPanelTemplate>
<HierarchicalDataTemplate
x:Key="DataTemplateForLayerAboveHorizontalItems"
DataType="{x:Type viewModel:ThingHavingHorizontalItems}"
ItemsSource="{Binding HorizontalItems}"
ItemTemplate="{StaticResource DataTemplateForLayerWithHorizontalItems}">
<HierarchicalDataTemplate.ItemContainerStyle>
<Style
TargetType="TreeViewItem">
<Setter
Property="ItemsPanel"
Value="{StaticResource ItemsPanelForHorizontalItems}"/>
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
<ContentControl
Content="{Binding}"
ContentTemplate="{StaticResource DataTemplateForThingHavingHorizontalItems}"/>
</HierarchicalDataTemplate>
上设置ItemsPanel
属性即可使用水平TreeView
。