2011-03-07 78 views
0

相對結合我有一個包含一個ObservableCollection一個ObservableCollection:WPF使用分層集合結構

public class BooksDetailModel 
{ 
    public BookModel Book{ get; set; } 
    public ObservableCollection<AuthorModel> Authors { get; set; } 
} 

屬性,視圖模型:

public ObservableCollection<BooksDetailModel> Books { get; set; } 

我想使這個在這樣的列表框:

Book1

  • 作者1
  • Author2

第二冊

  • 作者1

頂級綁定是容易的,但我有與內子集的麻煩。

XAML至今:

   <ListBox ItemsSource="{Binding Books}" BorderBrush="{x:Null}"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Vertical"> 
          <TextBlock Text="{Binding Book.Name}" FontSize="12" FontWeight="Bold" /> 
          <ListBox> 
           <ListBox.ItemTemplate> 
            <DataTemplate> 
             <TextBlock Text="{Binding ???, Path=Author.Name}" FontSize="10" /> 
            </DataTemplate> 
           </ListBox.ItemTemplate> 
          </ListBox> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

的任何建議??? - 將內部列表框項目源相對綁定到父項目源的作者集合。

+0

你應該使用HierarchicalDataTemplate – 2011-03-07 17:16:43

回答

4

您應該將內部ListBoxItemsSource綁定到Authors屬性。而在DataTemplate的結合將會更加簡單結合筆者的Name屬性:

<ListBox ItemsSource="{Binding Books}" BorderBrush="{x:Null}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Vertical"> 
       <TextBlock Text="{Binding Book.Name}" FontSize="12" FontWeight="Bold" /> 
       <ListBox ItemsSource="{Binding Authors}"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <TextBlock Text="{Binding Name}" FontSize="10" /> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

謝謝 - 我不斷地通過XAML結合的靈活性感到驚訝。我認爲這可能與RelativeSource有關。 – IUnknown 2011-03-07 18:21:47