2016-04-11 105 views
0

我对wpf非常感兴趣。我尝试在另一个datatemplate内创建嵌套的datatemplatelike this layoutdatatemplate)。我设法创建了一个数据模板,并在ObservableCollection中连接到"Object A",这非常有效。现在我需要在每个Object A中嵌套ObservableCollection以显示Object BObject C的列。但我真的不知道该怎么做,也找不到例子。如何使用嵌套的数据模板创建wpf模板(数据模板中的数据模板)

也许任何人都可以给我一个提示?

感谢和问候, 马琳

回答

0

假设你正在使用ListViewListBox显示的Object A集合(可以称之为ObjectACollection),以及Object A已命名的属性(在这个例子中)ObjectBCollectionObjectCCollection你可以这样做:

<UserControl xmlns:namespaceA="clr-namespace:MyProj.Models.ObjectANamespace" 
      xmlns:namespaceB="clr-namespace:MyProj.Models.ObjectBNamespace" 
      xmlns:namespaceC="clr-namespace:MyProj.Models.ObjectCNamespace" > 
    <ListBox ItemsSource="{Binding ObjectACollection}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate DataType="namespaceA:ObjectA"> 
       <StackPanel Orientation="Horizontal"> 
        <ListBox ItemsSource="{Binding ObjectBCollection}"> 
         <ListBox.ItemTemplate> 
          <DataTemplate DataType="namespaceB:ObjectB"> 
           <TextBlock Text="{Binding ObjectBProperty}"/> 
          </DataTemplate> 
         </ListBox.ItemTemplate> 
        </ListBox> 
        <ListBox ItemsSource="{Binding ObjectCCollection}"> 
         <ListBox.ItemTemplate> 
          <DataTemplate DataType="namespaceC:ObjectC"> 
           <TextBlock Text="{Binding ObjectCProperty}"/> 
          </DataTemplate> 
         </ListBox.ItemTemplate> 
        </ListBox> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</UserControl>