WPF嵌套ListView ItemsSource

时间:2014-04-20 20:17:49

标签: c# wpf listview windows-store-apps

我有以下数据模型:

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) - 但我不知道要为nester ListView设置什么作为ItemsSource。

1 个答案:

答案 0 :(得分:3)

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

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