【问题标题】:Nesting WPF HierarchicalDataTemplates in a treeview在树视图中嵌套 WPF HierarchicalDataTemplates
【发布时间】:2018-04-04 14:05:28
【问题描述】:

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

How to display JSON in WPF TreeView

但我遇到了具有嵌套集合的 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 的一个示例。

【问题讨论】:

    标签: json wpf nested treeview hierarchicaldatatemplate


    【解决方案1】:

    你必须有嵌套模板...

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多