如何显示WPF中所有子节点的treeviewItem扩展器箭头显示?

时间:2018-08-28 11:16:31

标签: c# wpf wpf-controls

我附上了我的问题屏幕截图。因此,请参考它,我只需要在树状视图中展开箭头图标,即可在首次加载时对所有节点可见。

My Problem Image

问题是树视图项没有子项意味着箭头不可见。 但初始时间负载我需要该箭头可见。用户展开后,它将隐藏。

根具有子节点。但是其他人没有。因此,只有我可以加载扩展时间。

1 个答案:

答案 0 :(得分:0)

此行为放在TreeViewItem的ControlTemplate中。仅有一种更改方式-更改TreeViewItem的控件模板。

我使用了ToggleButton和TreeViewItem的默认模板和样式。

您应该只在TreeViewItem的ControlTemplate中注释一个触发器:

<Trigger Property="HasItems" Value="False">
    <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
</Trigger>

此结果视图: Result view

下面,我粘贴所有样式和模板。

ToggleButton的样式:

<Style TargetType="{x:Type ToggleButton}" x:Key="TreeViewItemToggleButtonStyle">
        <Setter Property="Focusable" Value="False"/>
        <Setter Property="Width" Value="16"/>
        <Setter Property="Height" Value="16"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border Background="Transparent" Height="16" Padding="5" Width="16">
                        <Path x:Name="ExpandPath" Data="M0,0 L0,6 L6,0 z" Fill="White" Stroke="#FF818181">
                            <Path.RenderTransform>
                                <RotateTransform Angle="135" CenterY="3" CenterX="3"/>
                            </Path.RenderTransform>
                        </Path>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="RenderTransform" TargetName="ExpandPath">
                                <Setter.Value>
                                    <RotateTransform Angle="180" CenterY="3" CenterX="3"/>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Fill" TargetName="ExpandPath" Value="#FF595959"/>
                            <Setter Property="Stroke" TargetName="ExpandPath" Value="#FF262626"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Stroke" TargetName="ExpandPath" Value="#FF27C7F7"/>
                            <Setter Property="Fill" TargetName="ExpandPath" Value="#FFCCEEFB"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True"/>
                                <Condition Property="IsChecked" Value="True"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Stroke" TargetName="ExpandPath" Value="#FF1CC4F7"/>
                            <Setter Property="Fill" TargetName="ExpandPath" Value="#FF82DFFB"/>
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

TreeViewItem的ControlTemplate:

<ControlTemplate TargetType="{x:Type TreeViewItem}" x:Key="ItemTemplate">
        <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="{DynamicResource TreeViewItemToggleButtonStyle}"/>
            <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" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
            </Border>
            <ItemsPresenter x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsExpanded" Value="False">
                <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
            </Trigger>
            <!-- Commented 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.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>

TreeViewItem的样式:

<Style TargetType="TreeViewItem">
    <EventSetter Event="Expanded" Handler="TreeViewItem_Expanded"/>
    <Setter Property="Template" Value="{DynamicResource ItemTemplate}"/>
</Style>

窗口:

<Grid>
    <TreeView>
        <TreeViewItem Header="Root" IsExpanded="True">
            <TreeViewItem Header="Inner 1"/>
            <TreeViewItem Header="Inner 2"/>
        </TreeViewItem>
    </TreeView>
</Grid>