2014-03-25 133 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