在树视图中嵌套 WPF HierarchicalDataTemplates

Nesting WPF HierarchicalDataTemplates in a treeview

我正在解析 JSON 并将其显示在树视图中,如该问题中所述...

但我 运行 进入具有嵌套集合的 JSON 并且我的代码不会显示它。我可以显示一个字符串或一个子项列表,但是如果其中一个子项也包含它自己的子项,它们将不会显示。

如何显示n个嵌套项?

这是我的 XAML...

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApp2"
    Title="Window1" Height="300" Width="300">

<Window.Resources>
    <local:ValConv x:Key="valConv"/>
</Window.Resources>
<Grid>
    <TreeView x:Name="tView">

        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Value, Converter={StaticResource valConv}}" >
                <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" Foreground="Red"/>
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
            <TextBlock Text="{Binding Key}"/>

        </HierarchicalDataTemplate>

    </TreeView.ItemTemplate>            

</TreeView>
</Grid>

这里是 JSON 的例子。

您必须有嵌套模板...

<!--Based treeview item style-->

    <Style x:Key="TreeItemItemStyle" TargetType="{x:Type TreeViewItem}">

        <Setter Property="Foreground" Value="{DynamicResource CbrForegroundBrush}" />

        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />

        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />

        <Style.Triggers>

            <Trigger Property="IsSelected" Value="True">

                <Setter Property="FontWeight" Value="Bold" />

            </Trigger>

        </Style.Triggers>

    </Style>



    <!--DRIVE treeview item style-->

    <Style x:Key="DriveItemStyle" TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource TreeItemItemStyle}">

    </Style>



    <!--FOLDER treeview item style-->

    <Style x:Key="DirectoryItemStyle" TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource TreeItemItemStyle}">

        <Setter Property="FontStyle" Value="Normal"/>

    </Style>



    <!--FILE treeview item style-->

    <Style x:Key="FileItemStyle" TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource TreeItemItemStyle}">

        <Setter Property="FontStyle" Value="Normal"/>

    </Style>



    <!--treeview item style SELECTOR-->

    <Selectors:SysObjectItemStyleSelector x:Key="SysObjectItemStyleSelector"

        DriveStyle="{StaticResource DriveItemStyle}"

        DirectoryStyle="{StaticResource DirectoryItemStyle}" 

        FileStyle="{StaticResource FileItemStyle}" />



    <!--DRIVE treeview item template-->

    <HierarchicalDataTemplate DataType="{x:Type ViewModels:SysDriveViewModel}" ItemsSource="{Binding Children}">

        <StackPanel Orientation="Horizontal">

            <Image Width="16" Height="16" Margin="3,0"

                   Source="{Binding Path=Type, Converter={x:Static Converters:TypeToImageConverter.Instance}}" />

            <TextBlock Text="{Binding Name}"/>

        </StackPanel>

    </HierarchicalDataTemplate>



    <!--FOLDER treeview item template-->

    <HierarchicalDataTemplate DataType="{x:Type ViewModels:SysDirectoryViewModel}" ItemsSource="{Binding Children}">

        <StackPanel Orientation="Horizontal">

            <Image Width="16" Height="16" Margin="3,0"

                   Source="{Binding Path=Type, Converter={x:Static Converters:TypeToImageConverter.Instance}}" />

            <TextBlock Text="{Binding Name}"/>

        </StackPanel>

    </HierarchicalDataTemplate>



    <!--FILE treeview item template-->

    <DataTemplate DataType="{x:Type ViewModels:SysFileViewModel}">

        <StackPanel Orientation="Horizontal">

            <Image Width="16" Height="16" Margin="3,0" 

                   Source="{Binding Path=Type, Converter={x:Static Converters:TypeToImageConverter.Instance}}" />

            <TextBlock Text="{Binding Name}"/>

        </StackPanel>

    </DataTemplate>

等等 - 查看示例 https://wpf.2000things.com/tag/hierarchicaldatatemplate/