问题
I have a following data model:
class Item{
public string Name{get;set;}
public ObservableCollection<SubItem> SubItems {get;set;}
}
class SubItem{
public string Name {get;set;}
}
I have a ListView
that shows an ObservableCollection fine as:
<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>
However, I'd like to have a horizontal list of items (the nested ListView) - but I don't know what to set as ItemsSource for the nester ListView.
回答1:
Assuming that outer ListView
is bound to list of Item
then inner ListView.ItemsSource
should be bound to SubItems
property
<ListView x:Name="lvResult" ...>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" FontWeight="Bold"/>
<ListView ... ItemsSource="{Binding SubItems}">
来源:https://stackoverflow.com/questions/23187130/wpf-nested-listview-itemssource