期望输出
--- 1个
-------- 11个
--- 2
-------- 22个
--- 3
-------- 33
实际输出:
---(空节点)
-------- 1个
----------- 11个
-------- 2
----------- 22个
-------- 3
----------- 33
如果我向treeviewitem添加标题,我会得到以下结果:
---头
-------- 1个
----------- 11个
-------- 2
----------- 22个
-------- 3
----------- 33个
以下是XAML代码
<TreeView>
<TreeViewItem ItemsSource="{Binding Groups,Mode=OneWay}" Header="Root">
<TreeViewItem.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical" Height="Auto" Width="Auto" Background="Transparent" VirtualizingStackPanel.VirtualizationMode="Recycling" VirtualizingStackPanel.IsVirtualizing="True"/>
</ItemsPanelTemplate>
</TreeViewItem.ItemsPanel>
<TreeViewItem.Resources>
<Style TargetType="{x:Type TreeViewItem}" >
<Setter Property="ItemsSource" Value="{Binding Members,Mode=OneWay}"/>
<Setter Property="DisplayMemberPath" Value="Number"/>
</Style>
</TreeViewItem.Resources>
</TreeViewItem>
</TreeView>
答案 0 :(得分:0)
当您分配TreeView本身的ItemsSource时,只需更深入一步,以便根的子项成为树视图的项目。这是它的完成方式
<TreeViewItem ItemsSource="{Binding Groups,Mode=OneWay, XPath=./*}" Header="Root" />
注意 XPath !