You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF TreeViewItem样式问题:三级子项连线需终止于四级子项

TreeView层级连线终止需求处理

需求说明

3LvSub下方的连线当前延伸至Lv5,需调整为在4LvSub处终止。

处理规则

  • 移除原TreeView.Lv3样式中的Style.Triggers部分
  • 基于修改后的Lv3样式,为4Lv样式新增一个Rectangle控件

修改后的TreeView.Lv3样式

<Style x:Key="TreeView.Lv3"
       BasedOn="{StaticResource TreeView.Main}"
       TargetType="{x:Type TreeViewItem}">
    <Setter Property="Background" Value="Gray" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TreeViewItem}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" MinWidth="19" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="20" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Rectangle Grid.Row="1"
                               Width="1"
                               Margin="0,-5,1,9"
                               Fill="White"
                               SnapsToDevicePixels="True"
                               Stroke="#8F8F8F"
                               StrokeDashArray="0,2"
                               StrokeDashCap="Square" />
                    <ToggleButton x:Name="Expander"
                                  Grid.Column="0"
                                  ClickMode="Press"
                                  IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                  Style="{StaticResource ExpandCollapseToggleStyle_Plus}" />
                    <Border x:Name="Bd"
                            Grid.Column="1"
                            Padding="{TemplateBinding Padding}"
                            Background="Transparent"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="true">
                        <StackPanel Orientation="Horizontal">
                            <ContentPresenter x:Name="PART_Header"
                                              HorizontalAlignment="Center"
                                              VerticalAlignment="Center"
                                              ContentSource="Header"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </StackPanel>
                    </Border>
                    <ItemsPresenter x:Name="ItemsHost"
                                    Grid.Row="1"
                                    Grid.Column="1"
                                    Grid.ColumnSpan="2" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

新增的4Lv样式

<Style x:Key="TreeView.Lv4"
       BasedOn="{StaticResource TreeView.Lv3}"
       TargetType="{x:Type TreeViewItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TreeViewItem}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" MinWidth="19" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="20" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <!-- 保留Lv3的垂直连线 -->
                    <Rectangle Grid.Row="1"
                               Width="1"
                               Margin="0,-5,1,9"
                               Fill="White"
                               SnapsToDevicePixels="True"
                               Stroke="#8F8F8F"
                               StrokeDashArray="0,2"
                               StrokeDashCap="Square" />
                    <!-- 新增的Rectangle,用于终止下方连线(可根据布局调整Grid位置和样式) -->
                    <Rectangle Grid.Row="2"
                               Grid.Column="0"
                               Width="19"
                               Height="1"
                               Margin="0,0,0,0"
                               Fill="#8F8F8F"
                               SnapsToDevicePixels="True" />
                    <ToggleButton x:Name="Expander"
                                  Grid.Column="0"
                                  ClickMode="Press"
                                  IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                  Style="{StaticResource ExpandCollapseToggleStyle_Plus}" />
                    <Border x:Name="Bd"
                            Grid.Column="1"
                            Padding="{TemplateBinding Padding}"
                            Background="Transparent"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="true">
                        <StackPanel Orientation="Horizontal">
                            <ContentPresenter x:Name="PART_Header"
                                              HorizontalAlignment="Center"
                                              VerticalAlignment="Center"
                                              ContentSource="Header"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </StackPanel>
                    </Border>
                    <ItemsPresenter x:Name="ItemsHost"
                                    Grid.Row="1"
                                    Grid.Column="1"
                                    Grid.ColumnSpan="2" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

内容的提问来源于stack exchange,提问作者hong_ar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 14:52:27