I am trying to use a HierarchicalDataTemplate to recursively create expanders with items in them but when I use the HierarchicalDataTemplate I only get the first level of items displayed.
Please let me know if you need anyore information.
Heres the what the xaml would look like if I was writing by hand:
<GroupBox Header="SectionHeader">
<StackPanel >
<Expander VerticalAlignment="Top" Header="SubSectionHeader">
<StackPanel>
<Expander VerticalAlignment="Top" Header="SubSectionHeader" Margin="10,0,0,0">
<StackPanel>
etc......
</StackPanel>
</Expander>
</Expander>
</StackPanel>
</GroupBox>
Heres what I have so far.
Xaml:
<ItemsControl Name="lstMain" ItemsSource="{Binding Sections}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<GroupBox Header="{Binding Section.SectionName}">
<ItemsControl ItemsSource="{Binding SubSections}" ItemTemplate="{StaticResource BinderTemplate}" />
</GroupBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<HierarchicalDataTemplate x:Key="BinderTemplate" ItemsSource="{Binding Path=SubSections}" DataType="{x:Type local:SubSectionViewModel}">
<StackPanel>
<Expander Header="{Binding SubSection.SubSectionName}"/>
</StackPanel>
</HierarchicalDataTemplate>
Data Classes:
class TopViewModel
{
ObservableCollection<SectionViewModel> _sections = new ObservableCollection<SectionViewModel>();
public ObservableCollection<SectionViewModel> Sections
{
get
{
return _sections;
}
set
{
_sections = value;
}
}
}
public class SectionViewModel
{
ObservableCollection<MaterialViewModel> _materials = new ObservableCollection<MaterialViewModel>();
ObservableCollection<SubSectionViewModel> _subSections = new ObservableCollection<SubSectionViewModel>();
Section _section;
public Section Section
{
get
{
return _section;
}
set
{
_section = value;
}
}
public string MaterialName
{
get { return Section.SectionName; }
set { Section.SectionName = value; }
}
public ObservableCollection<MaterialViewModel> Materials
{
get
{
return _materials;
}
set
{
_materials = value;
}
}
public ObservableCollection<SubSectionViewModel> SubSections
{
get
{
return _subSections;
}
set
{
_subSections = value;
}
}
}
public class SubSectionViewModel
{
ObservableCollection<MaterialViewModel> _materials = new ObservableCollection<MaterialViewModel>();
ObservableCollection<SubSectionViewModel> _subSections = new ObservableCollection<SubSectionViewModel>();
SubSection _subSection;
public ObservableCollection<MaterialViewModel> Materials
{
get
{
return _materials;
}
set
{
_materials = value;
}
}
public ObservableCollection<SubSectionViewModel> SubSections
{
get
{
return _subSections;
}
set
{
_subSections = value;
}
}
public SubSection SubSection
{
get
{
return _subSection;
}
set
{
_subSection = value;
}
}
}