WPF树视图,如何更改缩进 [英] WPF Tree view, how to change indention

查看:165
本文介绍了WPF树视图,如何更改缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Treeview基本上具有文件夹节点,并且在不包含其他项目的项目下一层。

My Treeview basically has "folder" nodes, and one level below items which do NOT contain other items.

因此,展开/折叠图标的空间不是必需(在2级上)。我可以放弃此图标空间,从而减少缩进吗?这些项目(在示例机场中)应向左移动一些像素。

Therefor the space for the expand / collapse icons is not required (on level 2). Can I give up this icon space and hence reduce the indention. The items (in the example "airports") shall be shifted some pixels to the left.

重要提示:基本上是在寻找代码解决方案(C#),而不是XAML版本。

Important: Basically looking for the code solution (C#), not the XAML version.

推荐答案

您真正想要做的是编辑HierarchicalDataTemplate并更改其行为方式。下面的页面对编辑分层数据模板

Really what you want to do is edit the HierarchicalDataTemplate and change the way it behaves. The following page, has a pretty good high level view of editing the Hierarchical Data Template.

我还发现了一开始就非常不错。虽然这两个页面都没有具体说明要做什么,但是您实质上是在更改项目演示者中的布局属性。

I have also found this one to be pretty good to start out with. While neither of the pages specifically say what to do, you are essentially changing the layout properties in the items presenter.

糟糕,我不正确。不是HierarchicalDataTemplate,而是TreeViewItem模板。

Whoops, I was incorrect. Not HierarchicalDataTemplate, but TreeViewItem template.

请参见以下示例。如果您知道将不会有任何第三级节点,这就是最简单的方法。

See below for an example. This is just the simplest way to do it if you KNOW that there are not going to be any third level nodes.

请特别注意名为ItemsHost的ItemsPresenter元素。它的边距为-12,0,0,0。这意味着其左边界为负,因此沿左方向溢出到包含它的网格列中。因此,所有子节点将向左拉一些。如果将来有三级节点,它们也将被拉到左侧。如果您不希望这样做,则必须为不同级别的节点提供不同的模板。但这不在此答案的范围内。

Pay special attention to the ItemsPresenter element named ItemsHost. It has a margin of -12,0,0,0. That means that its left margin is negative and thus spills out of the grid column containing it in the left direction. Thus all of the child nodes will be pulled to the left a little. If you have third level nodes in the future, they also will be pulled to the left. If you don't want that, then you will have to supply different templates for different levels of nodes. But that is outside the scope of this answer.

<Style x:Key="TreeViewItemStyle1" TargetType="{x:Type TreeViewItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TreeViewItem}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition MinWidth="19" Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/>
                    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                        <ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                    <ItemsPresenter x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1" Margin="-12,0,0,0"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsExpanded" Value="false">
                        <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
                    </Trigger>
                    <Trigger Property="HasItems" Value="false">
                        <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" TargetName="Bd" 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 Property="Background" TargetName="Bd" 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.Triggers>
        <Trigger Property="VirtualizingStackPanel.IsVirtualizing" Value="true">
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel/>
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

这篇关于WPF树视图,如何更改缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆