0

我有一个带有两个相似子树的分层数据结构:

Iteration:
   string  Name
   string  Image
   ObservableCollection<Iteration>  SubIterations
   ObservableCollection<Iteration>  BacklogIterations

我想使用两个树视图来呈现树的两个稍微不同的视图。

  • Tree1:显示子迭代
  • Tree2:显示 BacklogIterations

元素应该显示相同,但​​需要使用不同的属性来拾取子项。

即我想参数化 HierarchicalDataTemplate 中的 ItemsSource:

<HierarchicalDataTemplate x:Key="IterationItem"
  ItemsSource="{Binding SubIterations}"
  >
  <StackPanel Orientation="Horizontal">
    <Image Width="32" Height="32" 
       Margin="3,0" Source="{Binding Picture}" />
    <TextBlock Text="{Binding Name}" />
  </StackPanel>
</HierarchicalDataTemplate>

<TreeView ItemsSource="{Binding RootSprintIteration}">
</TreeView>

<TreeView ItemsSource="{Binding RootBacklogIteration}">
</TreeView>

如何让第二棵树中的 HierarchicalDataTemplate 使用不同的 ItemsSource?我不想复制整个 HierarchicalDataTemplate 定义只是为了更改 ItemsSource。

4

1 回答 1

1

将数据的 Viauslization 放在 DataTemplate 中并创建两个 HierarchicalDataTemplates,在其中将 ItemTemplate 设置为创建的 DataTemplate。没有理由创建两个 HierarchicalDataTemplate。它们为您提供了您想要的参数化,并允许您对两者使用相同的 ItemTemplate

于 2011-04-28T12:44:11.940 回答