WPF nested ListView ItemsSource

本文关键字:ItemsSource ListView nested WPF | 更新日期: 2023-09-27 17:59:04

我有以下数据模型:

class Item{
  public string Name{get;set;}
  public ObservableCollection<SubItem> SubItems {get;set;}
} 
class SubItem{
  public string Name {get;set;}
}

我有一个ListView,它将ObservableCollection精细显示为:

        <ListView x:Name="lvResult" Background="DeepPink" Grid.Row="1" ItemsSource="{Binding}">
                <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding }" FontWeight="Bold"/>
                        <ListView Background="Black" Margin="8,0,0,0">
                            <ListView.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal"/>
                                </ItemsPanelTemplate>
                            </ListView.ItemsPanel>
                        </ListView>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

然而,我希望有一个水平的项目列表(嵌套的ListView),但我不知道该为嵌套的ListView设置什么作为ItemsSource。

WPF nested ListView ItemsSource

假设外部ListView绑定到Item的列表,则内部ListView.ItemsSource应绑定到SubItems属性

<ListView x:Name="lvResult" ...>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding}" FontWeight="Bold"/>
                <ListView ... ItemsSource="{Binding SubItems}">