没有树形结构我可以拥有Treeview吗?

时间:2009-10-06 17:18:11

标签: wpf treeview itemscontrol hierarchicaldatatemplate

有人能告诉我如何模拟树视图控件以使它看起来不像树吗?基本上,我想删除各个级别的缩进以及允许扩展和折叠的+/-。

实际要求是我有一个分层数据模板,它绑定到一个本质上是递归的对象集合。我希望以平面的方式在屏幕上显示这些对象。最好的解决方案是使层次结构数据模板与itemscontrol一起使用。但遗憾的是,我认为itemscontrol不了解分层模板。所以我被迫使用树视图控件并删除所有“树”功能。

任何建议都会很有帮助..

3 个答案:

答案 0 :(得分:2)

您需要修改TreeViewItem的模板。我已经为你做了,只需在你的窗口中复制/粘贴下面的代码。虽然我非常建议你学习ControlTemplates以及如何修改它们,因为如果你要使用WPF。这非常有用。

你走了:

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

答案 1 :(得分:2)

您可以查看thisthis文章,了解如何完全更改树视图的呈现:

alt text

答案 2 :(得分:1)

您可以使用模板执行此操作,但如果数据不需要在层次结构中,那么在ViewModel中展平它更有意义 - 可能使用AutoMapper