2014-03-25 130 views
0

在我有一些問題,結合一個ListBox到集合的集合中的元素。讓我解釋一下:綁定一個ListBox到集合中的元素集合

我有一個集合,ObservableCollection<Test>命名testsCollection。每個測試都包含名爲LogEventsObservableCollection<LogEvent>。每個LogEvent都有一個Message,我需要在列表框中顯示。

我需要在每個「測試」中的每個「LogEvent」中顯示每個「消息」。它必須顯示在一個平面列表中,所以我使用了一個ListBox。

這裏是我的嘗試總結:

DataContext = testCollection; // testCollection is an ObservableCollection<Test> 

的,我把這個在XAML:

<ListBox ItemsSource="{Binding LogEvents}" ItemTemplate="{StaticResource stepItemTemplate}"> 

最後,這裏的ItemTemplate中,stepItemTemplate:

<DataTemplate x:Key="stepItemTemplate"> 
    <TextBlock Text="{Binding Message}"></TextBlock> 
</DataTemplate> 

這「有效」,但它只在第一個測試的LogEvents中顯示消息。但我需要顯示每個測試的每一個的LogEvent的每一個消息。而我不知道該怎麼再嘗試了:(

回答

1

你應該Usser的ItemsControl當你想這樣的情形,結合這樣的

<ListBox ItemsSource="{Binding testsCollection}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="{Binding Message}" FontSize="20" /> 
        <ItemsControl ItemsSource="{Binding LogEvents}" Margin="0 20 0 0"> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <Border BorderBrush="Blue" BorderThickness="2"> 
           <TextBlock Text="{Binding Message}" FontSize="20" /> 
          </Border> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ItemsControl> 

      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
+0

謝謝,我會盡力做到這一點! – Michael