我正在解析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的一个例子。
答案 0 :(得分: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/