C# 将列表框绑定到集合中集合的元素

C# 将列表框绑定到集合中集合的元素,c#,wpf,data-binding,binding,listbox,C#,Wpf,Data Binding,Binding,Listbox,将列表框绑定到集合中集合的元素时遇到一些问题。。让我解释一下: 我有一个集合,observedcollection名为testscolection。每个测试都包含一个名为LogEvents的ObservableCollection。每个LogEvent都有一条消息,我需要在列表框中显示该消息 我需要在每个“测试”中的每个“日志事件”中显示每个“消息”。它必须显示在一个平面列表中,所以我使用的是一个列表框 以下是我尝试的总结: DataContext = testCollection; // te

将列表框绑定到集合中集合的元素时遇到一些问题。。让我解释一下:

我有一个集合,
observedcollection
名为
testscolection
。每个测试都包含一个名为
LogEvents
ObservableCollection
。每个
LogEvent
都有一条
消息,我需要在列表框中显示该消息

我需要在每个“测试”中的每个“日志事件”中显示每个“消息”。它必须显示在一个平面列表中,所以我使用的是一个列表框

以下是我尝试的总结:

DataContext = testCollection; // testCollection is an ObservableCollection<Test>
DataContext=testCollection;//testCollection是一个可观察的集合
我把这个放在XAML中:

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

最后,这里是ItemTemplate,stepItemTemplate:

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


这“起作用”,但它仅在第一次测试的日志事件中显示消息。但是我需要显示每个测试的每个日志事件的每个消息。。我不知道该尝试什么:(

当你想绑定这样的场景时,你应该使用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>

谢谢,我会尽力做到这一点!