- TreeView の ItemsSource に全体の配列をBindし、 HierarchicalDataTemplate の ItemsSource に、子要素をBindする
- 各項目のスタイルは、TreeView.ItemContainerStyle に設定する
- 子要素が1つでもあれば、展開(>)ボタンが表示される
MainWindow.xaml
<Window x:Class="WpfApp7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp7"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TreeView ItemsSource="{Binding Mode=OneWay}">
<TreeView.Style>
<!-- TreeView自体のスタイル -->
<Style TargetType="{x:Type TreeView}" BasedOn="{StaticResource MaterialDesignTreeView}">
<Setter Property="Background" Value="#FFF0F0F0"/>
</Style>
</TreeView.Style>
<TreeView.ItemContainerStyle>
<!-- TreeViewの各項目のスタイル -->
<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource MaterialDesignTreeViewItem}">
<Setter Property="Padding" Value="0"/>
<Setter Property="Background" Value="{Binding Path=BackColor, Mode=OneTime}"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<!-- HierarchicalDataTemplate の ItemSource に、子階層をBind -->
<HierarchicalDataTemplate DataType="local:TreeDataStructure" ItemsSource="{Binding Path=SubData, Mode=OneWay}">
<Label Content="{Binding Path=LabelTest, Mode=OneWay}" Foreground="{Binding Path=ForeColor, Mode=OneTime}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>