本文介绍了没有树形结构的树形视图可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何为树视图控件创建模板,以使其看起来不像树吗?基本上,我想删除各种级别的缩进以及允许扩展和折叠的+/-.

Can someone tell me how I can template a treeview control so that it doesn't look like a tree? Basically, I want to remove the indents at various levels as well as the +/- that allow for expansion and collapse.

实际要求是我有一个分层数据模板,该模板绑定到本质上是递归的对象集合.我想以平面方式在屏幕上显示这些对象.最好的解决方案是使分层数据模板与ItemsControl一起使用.但是可悲的是,我相信ItemsControl无法理解分层模板.因此,我被迫使用树视图控件并剥离所有树"功能.

The actual requirement is that I have a hierarchical data template that is bound to an object collection that is recursive in nature. And I want to display these objects on screen in a flat manner. The best solution would be to get the hierarchical data template to work with an itemscontrol. But sadly, i believe that the itemscontrol does not understand hierarchical templates. So I am forced to use a treeview control and strip all the "tree" features.

任何建议都会很有帮助.

Any suggestions will be mighty helpful..

推荐答案

您需要修改TreeViewItem的模板.我已经为您完成了此操作,只需在下面的窗口中复制/粘贴代码即可.尽管我极力建议您研究 ControlTemplates 以及如何修改它们,因为如果您要使用WPF.非常有用.

You need to modify the TreeViewItem's template. I already did it for you, just copy/paste code below in your Window. Although I extremely recommend you study ControlTemplates and how to modify them since if you're gonna be using WPF. It's EXTREMELY useful.

您在这里:

<Window.Resources>
        <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
        <SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />
        <SolidColorBrush x:Key="GlyphBrush" Color="#444" />

        <!-- TreeView -->
        <Style x:Key="{x:Type TreeView}" TargetType="TreeView">
            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TreeView">
                        <Border
                            Name="Border"
                            CornerRadius="1"
                            Background="{StaticResource WindowBackgroundBrush}"
                            BorderBrush="{StaticResource SolidBorderBrush}"
                            BorderThickness="1">
                            <ScrollViewer
                              Focusable="False"
                              CanContentScroll="False"
                              Padding="4">
                                <ItemsPresenter/>
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!-- TreeViewItem -->
        <Style x:Key="TreeViewItemFocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border>
                            <Rectangle Margin="0,0,0,0"
                               StrokeThickness="5"
                               Stroke="Black"
                               StrokeDashArray="1 2"
                               Opacity="0"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment,
                    RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment,
                    RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="Padding" Value="1,0,0,0"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeViewItem}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Border Name="Bd"
                                Grid.Column="0"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}">
                                <ContentPresenter x:Name="PART_Header"
                                    ContentSource="Header"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                            </Border>
                            <ItemsPresenter x:Name="ItemsHost" Visibility="Visible"
                                Grid.Row="1"
                                Grid.Column="0"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="HasHeader" Value="false"/>
                                    <Condition Property="Width" Value="Auto"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="PART_Header" Property="MinWidth" Value="75"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="HasHeader" Value="false"/>
                                    <Condition Property="Height" Value="Auto"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="PART_Header" Property="MinHeight" Value="19"/>
                            </MultiTrigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="Bd" Property="Background"
                                    Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="true"/>
                                    <Condition Property="IsSelectionActive" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Bd" Property="Background"
                                        Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <TreeView>
            <TreeViewItem Header="Node 1">
                <TreeViewItem Header="Node 1.1">
                    <TreeViewItem Header="Node 1.1.1" />
                </TreeViewItem>
            </TreeViewItem>
            <TreeViewItem Header="Node 2">
                <TreeViewItem Header="Node 2.1">
                    <TreeViewItem Header="Node 2.1.1" />
                </TreeViewItem>
                <TreeViewItem Header="Node 2.2">
                    <TreeViewItem Header="Node 2.2.1" />
                </TreeViewItem>
            </TreeViewItem>
        </TreeView>
    </Grid>
</Window>

这篇关于没有树形结构的树形视图可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:26
查看更多