我有一个与 HierarchicalDataTemplate 相关的问题。是否可以通过代码继续添加 HierarchicalDataTemplate 或者没有办法做到这一点。我问这个是因为在我的情况下,我不知道我是否需要比现在更多的子节点。
下面你可以看到我的树视图:
<TreeView Background="#FFF0F0F0" BorderBrush="#FFE5E2DB" IsEnabled="{Binding isAllesEnabled}" ItemsSource="{Binding LijstRechten, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" Margin="0,0,0,0" Name="dgStamOverzichtGebruikerRechten" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling">
<TreeView.ItemTemplate>
<!-- Top Level -->
<HierarchicalDataTemplate ItemsSource="{Binding Childs}">
<CheckBox IsChecked="{Binding isChecked}" Content="{Binding naam}" />
<!-- 1st Child Level -->
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Childs}">
<CheckBox IsChecked="{Binding isChecked}" Content="{Binding naam}" />
<!-- 2nd Child Level -->
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Childs}">
<CheckBox IsChecked="{Binding isChecked}" Content="{Binding naam}" />
<!-- 3rd Child Level -->
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding isChecked}" Content="{Binding naam}" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
<Setter Property="Margin" Value="2"/>
<!--<Setter Property="IsSelected" Value="{Binding isSelected, Mode=TwoWay}"/>
<EventSetter Event="Selected" Handler="TreeViewSelectedItemChanged" />-->
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
提前致谢!