我想使用HierarchicalDataTemplate遞歸創建其中的項目的擴展器,但是當我使用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>
繼承人是我到目前爲止所。
的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>
數據類:
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;
}
}
}
Brilliant。謝謝:)我曾嘗試這樣做,但試圖將該模板用作靜態資源而不是動態資源並得到了編譯時錯誤:) – user589195