【问题标题】:WPF TreeView with horizontal orientation?水平方向的WPF TreeView?
【发布时间】:2008-12-12 20:40:15
【问题描述】:

改变 WPF 树视图方向的最糟糕的方法是什么。我想使用展开折叠功能从左到右而不是从上到下工作。 IE。当我单击树节点的展开按钮时,我会使其子节点出现在父节点的右侧,并且缩进应该自上而下地工作。此外,连接节点的垂直线现在必须是水平的。

【问题讨论】:

    标签: wpf treeview


    【解决方案1】:

    这是Josh Smith on CodeProject 的一篇很棒的文章,详细说明了如何做这种事情。

    【讨论】:

      【解决方案2】:

      要扩展John Smith's CodeProject article,如果您只想在树中的特定级别上进行水平布局(而不是像他的文章显示的那样在所有级别上),那么只需在TreeViewItem 上设置ItemsPanel 属性在您想要拥有StackPanel 的级别。

      一开始对我来说并不直观,但是您可以通过HierarchicalDataTemplateItemContainerStyle 属性来获取此属性,以获取您想要水平的图层上方的图层。

      像这样:

      <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>
      

      按照此模式,您可以为树中的任何单独层设置水平布局,根层除外。如果您希望根层是水平的,那么只需将TreeView 上的ItemsPanel 属性设置为使用水平的StackPanel

      【讨论】:

      • 我应该补充一点,扩展一个与其他方向不同的层会破坏 UI 虚拟化。如果您知道如何解决该问题,请参阅 this question
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-12
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多