Original WPF TreeView Hierarchicaldatatemplate
<StackPanelMargin= "0,0,0,0"> <stackpanel.resources> <hierarchicaldatatemplatex:key= "Montemp"DataType= "{x:type Local:monthviewmodel}"ItemsSource= "{Binding Fileinfoviewmodels}"> <TextBlockText="{Binding Monthdescription}"/> <hierarchicaldatatemplate.itemtemplate> <DataTemplateDataType="{x:type Local:fileinfoviewmodel}"> <TextBlockText="{Binding FileName}"/> </DataTemplate> </hierarchicaldatatemplate.itemtemplate> </hierarchicaldatatemplate> <hierarchicaldatatemplatex:key= "Yeartemp"DataType= "{x:type Local:yearviewmodel}"ItemsSource= "{Binding Monthviewmodels}"ItemTemplate="{StaticResource Montemp}"> <TextBlockText="{Binding Yeardescription}"/> </hierarchicaldatatemplate> </stackpanel.resources> <TreeViewHeight= "+"Name= "Filetree"> <treeview.resources> <StyleTargetType="{x:type TreeViewItem}"> <Setter Property= "isexpanded"Value= "True"/> </Style> </treeview.resources> <TreeViewItemItemsSource="{Binding Treeviewmodels}"Header= "Back Up View"ItemTemplate="{StaticResource Yeartemp}"/> </TreeView> </StackPanel>
The datatype of hierarchicaldatatemplate is the binding of this layer, and ItemsSource is a set of objects bound to the layer, it can only use two layers collection, such as Montemp, if you want to use more than three layers, Need two hierarchicaldatatemplate, use ItemTemplate to refer to the other one.
Hierarchicaldatatemplate binding, the child nodes are directly bound DataSource object, not TreeViewItem, so to all default expansion, you want to set style with Treeview.resources.
WPF TreeView Hierarchicaldatatemplate