我正在尝试使用 HierarchicalDataTemplate 递归地创建包含项目的扩展器,但是当我使用 HierarchicalDataTemplate 时,我只能显示第一级项目。
如果您需要任何信息,请告诉我。
下面是我手写时 xaml 的样子:
<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>
这是我到目前为止所拥有的。
xml:
<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>
数据类:
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;
}
}
}