【问题标题】:Is is possible to make a TreeView look like a List?是否可以使 TreeView 看起来像列表?
【发布时间】:2012-01-26 18:40:57
【问题描述】:

我有一些分层数据需要显示为表格,我可以更改 TreeView 的样式以类似列表的方式显示数据吗?

所以而不是:

+Group1
  SubItem1
  SubItem2
+Group2
 +Group
   SubItem11
   SubItem12

我想要这个:

Group1
SubItem1
SubItem2
Group2
Group
SubItem11
SubItem12

我的代码如下所示:

<TreeView ItemsSource="{Binding RootItems}"
                  dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True">
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="true" />
                </Style>
            </TreeView.ItemContainerStyle>

            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type vm:CategoryViewModel}" 
                                          ItemsSource="{Binding Children}">
                    <v:Category DataContext="{Binding}" />
                </HierarchicalDataTemplate>
                <DataTemplate DataType="{x:Type vm:ItemViewModel}">
                    <v:Item DataContext="{Binding}" />
                </DataTemplate>
            </TreeView.Resources>
        </TreeView>

我知道我应该能够使用 TreeView 的样式,但我不知道从哪里开始,而且我找不到这方面的任何信息。

注意:Can I have a Treeview without the tree structure? 的副本

【问题讨论】:

标签: wpf treeview wpf-controls


【解决方案1】:

覆盖TreeViewItem 模板,例如

<TreeView.ItemContainerStyle>
    <Style TargetType="TreeViewItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TreeViewItem">
                    <StackPanel>
                        <ContentPresenter x:Name="PART_Header" ContentSource="Header"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        <ItemsPresenter />
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</TreeView.ItemContainerStyle>

【讨论】:

    【解决方案2】:

    您需要覆盖 default template of treeview 以扁平化您的层次结构。 这些链接可能会达到您的目的 -

    Can I have a Treeview without the tree structure?

    http://social.msdn.microsoft.com/Forums/en/wpf/thread/a5ce0a21-d34b-4f9e-be03-ac4c56840ca5

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2014-01-06
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多