2009-08-03 91 views
0

我有一个包含遵循此结构的对象的列表。这不是我正在使用的真正的课程,而是应该解释这个概念。WPF HierarchicalDataTemplate&ItemsControl

CLASSES

public class BaseType{} 
public class TypeA : BaseType{} 
public class TypeB: BaseType 
{ 
    public List<TypeA> TypeAList { get; private set; } 
} 

的ItemsControl的结合列表中是一个List<BaseType>

XAML现在

<ItemsControl> 
    <ItemsControl.Resources> 
     <HierarchicalDataTemplate DataType="{x:Type local:TypeB}" ItemsSource = "{Binding Path=TypeAList}"> 
      <DataTemplate.Resources> 
       <Style TargetType="TextBlock"> 
        <Setter Property="FontSize" Value="18"/> 
        <Setter Property="HorizontalAlignment" Value="Center"/> 
       </Style> 
      </DataTemplate.Resources> 
      <Grid> 
       <Ellipse Fill="Gold"/> 
       <StackPanel> 
        <TextBlock Margin="3,3,3,0" 
     Text="{Binding Path=Description}"/> 
        <TextBlock Margin="3,0,3,7" 
     Text="{Binding Path=Name}"/> 
       </StackPanel> 
      </Grid> 
     </HierarchicalDataTemplate> 
    <ItemsControl.Resources> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel></StackPanel> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ItemsControl> 

我期望看到的是所有类型A的对象在TypeB对象属性中找到要显示在Ite中msControl,而是我只看到TypeB对象,显示为为HierarchicalDataTemplate定义的样式。我在TreeView控件中使用了相同的数据模式,它显示的子项很好。

  • 你不能在ItemsControl中使用HierarchicalDataTemplate吗?
  • 你如何去在ItemsControl中显示父子关系?

回答

1

您确实需要研究模板并使用TreeView控件,或者构建自己的控件以处理分层数据。

在某些情况下,你可以设计自己的数据模板进行常规项目控制,其嵌套控件绑定到项目,如(伪)

<HierarchicalDataTemplate> 
    <Grid DataContext="{Binding}"> 
     <ListBox ItemsSource="{Binding TypeAList}" /> 
    </Grid> 
</HierarchicalDataTemplate> 

没有尝试过的代码上述

控制需要知道的HierarchicalDataTemplate的 - 并且在上面的例子中,控制被简单地使用它作为一个DataTemplate(HierarchicalDataTemplate从DataTemplate中导出用于在样式简单和的DependencyProperty的类型,该数据模板)。