WPF嵌套ListView ItemsSource [英] WPF nested ListView ItemsSource

查看:587
本文介绍了WPF嵌套ListView ItemsSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据模型:

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

class SubItem{
  public string Name {get;set;}
}

我有一个ListView,它显示的ObservableCollection罚款为:

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>

但是,我想要一个水平的项目列表(嵌套的ListView)-但我不知道要为嵌套ListView设置为ItemsSource.

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.

推荐答案

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

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}">

这篇关于WPF嵌套ListView ItemsSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆