我正在尝试使用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;
}
}
}发布于 2013-01-04 00:49:44
您的HierarchicalDataTemplate中缺少了一个关键部分-如何呈现子元素:
<HierarchicalDataTemplate x:Key="BinderTemplate"
ItemsSource="{Binding Path=SubSections}"
DataType="{x:Type local:SubSectionViewModel}">
<StackPanel>
<Expander Header="{Binding SubSection.SubSectionName}">
<ItemsControl Margin="5,0,0,0"
ItemsSource="{Binding SubSections}"
ItemTemplate="{DynamicResource BinderTemplate}"/>
</Expander>
</StackPanel>
</HierarchicalDataTemplate>编辑:不是想抢了@BDE的风头,但是他/她关于DataType的用法基本上是正确的--但这是你“简化”上面的XAML的方式:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<!-- normal template for sections -->
<DataTemplate DataType="{x:Type local:SectionViewModel}">
<GroupBox Header="{Binding Section.SectionName}">
<ItemsControl ItemsSource="{Binding SubSections}"/>
</GroupBox>
</DataTemplate>
<!-- hierarchical template for subsections -->
<HierarchicalDataTemplate
DataType="{x:Type local:SubSectionViewModel}">
<StackPanel>
<Expander Header="{Binding SubSection.SubSectionName}">
<ItemsControl Margin="5,0,0,0"
ItemsSource="{Binding SubSections}"/>
</Expander>
</StackPanel>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<!--
no need to specify ItemTemplate if WPF can suss out all the item types
-->
<ItemsControl Name="lstMain" ItemsSource="{Binding Sections}"/>
</Grid>
</Window>发布于 2013-01-04 00:49:07
HierarchicalDataTemplate的另一种选择是将ItemsControl添加到数据模板中的Expander,并在那里绑定ItemsSource。
此外,由于您在数据模板定义中指定了DataType,因此不必直接为顶层ItemsControl设置ItemTemplate by key name。
如果您像这样修改您的XAML,它可能会执行您想要的操作:
<DataTemplate DataType="{x:Type local:SubSectionViewModel}">
<StackPanel>
<Expander Header="{Binding SubSection.SubSectionName}">
<ItemsControl ItemsSource="{Binding SubSections}"/>
</Expander>
</StackPanel>
</DataTemplate>
<ItemsControl Name="lstMain" ItemsSource="{Binding Sections}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<GroupBox Header="{Binding Section.SectionName}">
<ItemsControl ItemsSource="{Binding SubSections}"/>
</GroupBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>发布于 2013-01-04 00:23:02
Section.SectionName应该是MaterialName?
查看线条:
<ItemsControl ItemsSource="{Binding SubSections}" ItemTemplate="{StaticResource BinderTemplate}" />和
<HierarchicalDataTemplate x:Key="BinderTemplate" ItemsSource="{Binding Path=SubSections}" DataType="{x:Type local:SubSectionViewModel}">我认为,如果第一行绑定是SubSections,那么第二行而不是
{Binding Path=SubSections} 你可能应该写下
{Binding}我在你的课上找不到SubSectionName。
请添加更多类
https://stackoverflow.com/questions/14142510
复制相似问题