WPF nested ListView ItemsSource

杀马特。学长 韩版系。学妹 提交于 2019-12-24 03:28:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!