在 TreeView 中隐藏 Expand/Collapse 图标

Hiding Expand/Collapse icon in TreeView

我使用 TreeView 创建一个通用的 Json 对象编辑器。但是,我想完全隐藏 expand/collapse 图标。我搜索了很多,但到目前为止找不到任何可行的解决方案。有没有一种简单的方法来覆盖适当的样式以摆脱 collapse/expand 功能?

<TreeView Name="m_TreeView" ItemTemplateSelector="{StaticResource templateSelector}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <Setter Property="IsExpanded" Value="True"/>
            <Setter Property="Focusable" Value="False"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

有没有关于如何在不创建全新 TreeViewItem 模板的情况下实现这一目标的想法?

折叠/展开 ToggleButton 是在 TreeViewItem 的控件模板中定义的,没有任何属性可以隐藏它们。因此,您必须调整 TreeViewItem 模板。

您可以创建默认 TreeViewItem 样式 using Visual Studio or Blend 的副本并进行调整。关键是删除 Expander ToggleButton 及其触发器。它的列必须保留用于缩进。 IsExpanded 属性 的 setter 默认展开所有项目。

<Style TargetType="{x:Type TreeViewItem}">
   <Setter Property="IsExpanded"
           Value="True" />
   <Setter Property="Background"
           Value="Transparent" />
   <Setter Property="HorizontalContentAlignment"
           Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
   <Setter Property="VerticalContentAlignment"
           Value="{Binding 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">
      <Setter.Value>
         <Style>
            <Setter Property="Control.Template">
               <Setter.Value>
                  <ControlTemplate>
                     <Rectangle />
                  </ControlTemplate>
               </Setter.Value>
            </Setter>
         </Style>
      </Setter.Value>
   </Setter>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type TreeViewItem}">
            <ControlTemplate.Resources>
               <local:DataTypeConverter x:Key="DataTypeConverter" />
            </ControlTemplate.Resources>
            <Grid>
               <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="19" />
                  <ColumnDefinition Width="Auto"
                                    MinWidth="0" />
                  <ColumnDefinition Width="Auto" />
                  <ColumnDefinition Width="*" />
               </Grid.ColumnDefinitions>
               <Grid.RowDefinitions>
                  <RowDefinition Height="Auto" />
                  <RowDefinition />
               </Grid.RowDefinitions>
               <Border x:Name="Bd"
                       Grid.Column="1"
                       Padding="{TemplateBinding Padding}"
                       Background="{TemplateBinding Background}"
                       BorderBrush="{TemplateBinding BorderBrush}"
                       BorderThickness="{TemplateBinding BorderThickness}"
                       SnapsToDevicePixels="true">
                  <ContentPresenter x:Name="PART_Header"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    ContentSource="Header"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
               </Border>
               <ItemsPresenter x:Name="ItemsHost"
                               Grid.Row="1"
                               Grid.Column="1"
                               Grid.ColumnSpan="2" />
            </Grid>
            <ControlTemplate.Triggers>
               <Trigger Property="IsExpanded"
                        Value="false">
                  <Setter TargetName="ItemsHost"
                          Property="Visibility"
                          Value="Collapsed" />
               </Trigger>
               <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.InactiveSelectionHighlightBrushKey}}" />
                  <Setter Property="Foreground"
                          Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}" />
               </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="VirtualizingPanel.IsVirtualizing"
               Value="true">
         <Setter Property="ItemsPanel">
            <Setter.Value>
               <ItemsPanelTemplate>
                  <VirtualizingStackPanel />
               </ItemsPanelTemplate>
            </Setter.Value>
         </Setter>
      </Trigger>
   </Style.Triggers>
</Style>

如果要去除根级项的缩进,可以参考这个