WPF 树视图,如何更改缩进

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13649183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:26:17  来源:igfitidea点击:

WPF Tree view, how to change indention

c#wpftreeview.net-4.5

提问by Horst Walter

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

我的 Treeview 基本上有“文件夹”节点,以及不包含其他项目的项目下方的一层。

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.

因此不需要为展开/折叠图标留出空间(在第 2 级)。我可以放弃这个图标空间从而减少缩进吗?项目(在示例“机场”中)应向左移动一些像素。

Example tree view

示例树视图

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

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

回答by CodeWarrior

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.

您真正想要做的是编辑 HierarchicalDataTemplate 并更改其行为方式。下面的页面有一个很好的高级视图来编辑分层数据模板

I have also found this oneto 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.

我也发现这个非常适合开始。虽然这两个页面都没有具体说明要做什么,但您实际上是在更改项目展示器中的布局属性。

Edit

编辑

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

哎呀,我错了。不是 HierarchicalDataTemplate,而是 TreeViewItem 模板。

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.

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

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.

请特别注意名为 ItemsHost 的 ItemsPresenter 元素。它的边距为 -12,0,0,0。这意味着它的左边距是负的,因此从包含它的网格列向左溢出。因此所有的子节点都会被拉到左边一点。如果以后有第三级节点,它们也会被拉到左边。如果您不希望那样,则必须为不同级别的节点提供不同的模板。但这超出了本答案的范围。

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