简体   繁体   English

如何将列表列表绑定到ListView控件

[英]How to bind a list of list to a ListView control

I have The following structure 我有以下结构

QPage class Object contains a List<List<QLine>> QLine object contains List<Qword> QPage类Object包含List<List<QLine>> QLine对象包含List<Qword>

every list of words constructs a line and every list of lines consists a group(paragraph) and every list of paragraphs consists a page. 每个单词列表构成一行,每个行列表包含一个组(段落),每个段落列表都包含一个页面。

I want to bind the page to structure like this in XAML 我想在XAML中将页面绑定到这样的结构

<ListView>
    <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
            <TextBlock>                 
            </TextBlock>
        </StackPanel>
    </StackPanel>
</ListView>

where each item of the ListView is a paragraph( List<QLine> ) and each vertical stack panel holds an item of the List<QLine> and each item of the horizontal stack panel holds an item of the List<Qword> and the texblock is bound to Qword.text property. 其中ListView的每个项目都是一个段落( List<QLine> ),每个垂直堆栈面板都包含List<QLine>项目,水平堆栈面板的每个项目都包含List<Qword>的项目,而texblock是绑定到Qword.text属性。 I have no idea how to do such binding from the XAML code. 我不知道如何从XAML代码中进行这样的绑定。

Hopefully I did not miss some list but this should work. 希望我没有错过一些列表,但这应该工作。 Basically it's a ListBox that hosts List<List<QLine>> (called it QPageList ). 基本上它是一个ListBox ,它承载List<List<QLine>> (称为QPageList )。 Then you have ItemsControl that hosts each List<QLine> in vertical panel and finally there is another ItemsControl that hosts List<Qword> from QLine (called it QwordList ) where each QWord is displayed as TextBlock on horizontal StackPanel 然后你有ItemsControl在垂直面板中托管每个List<QLine> ,最后还有另一个ItemsControlQLine托管List<Qword> (称为QwordList ),其中每个QWord在水平StackPanel上显示为TextBlock

<!-- ItemsSource: List<List<QLine>>, Item: List<QLine> -->
<ListBox ItemsSource="{Binding QPageList}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <!-- ItemsSource: List<QLine>, Item: QLine -->
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <!-- ItemsSource: QLine.List<QWord>, Item: QWord --> 
                        <ItemsControl ItemsSource="{Binding QwordList}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <!-- Item: QWord -->
                                    <TextBlock Text="{Binding text}"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

What you are looking for is ListView.ItemTemplate . 您正在寻找的是ListView.ItemTemplate Basically, you need to provide your list with a way to understand the nested data structure of your rows. 基本上,您需要为列表提供一种方法来了解行的嵌套数据结构。

Here is a good tutorial to get you started on ItemTemplates . 这是一个很好的教程,可以帮助您开始使用ItemTemplates

Once your list has an item template then you just bind the ListView directly to your data source and that's it. 一旦你的列表有一个项目模板,那么你只需将ListView直接绑定到你的数据源就可以了。

Hopefuly this will prove helpful. 希望这会证明是有帮助的。 Taken from here 取自这里

Authors (list)
 - - Name
 - - Books (list)
     | -  - Title
     | -  - Contents

Sample code : 示例代码:

  <Window.Resources>
    <DataTemplate x:Key="ItemTemplate1">
      <StackPanel>
        <TextBlock Text="{Binding Name}"/>
      </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="ItemTemplate2">
      <StackPanel>
        <TextBlock Text="{Binding Title}"/>
      </StackPanel>
    </DataTemplate>
  </Window.Resources>
  <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource AuthorDataSource}}">
    <ListBox x:Name="AuthorList" ItemTemplate="{DynamicResource ItemTemplate1}" ItemsSource="{Binding Authors }" >
    <ListBox x:Name="BookList"  DataContext="{Binding SelectedItem, ElementName=AuthorList}" ItemsSource="{Binding Books }" ItemTemplate="{DynamicResource ItemTemplate2}" />
    <TextBlock  x:Name="BookContent"   DataContext="{Binding SelectedItem, ElementName=BookList}"  Text="{Binding Contents }" />
  </Grid>

Forward to what Alex is saying, it'd be a good idea to Encapsulate (If that's the correct word here) the object within classes, ie: 转发到Alex所说的内容, Encapsulate (如果这是正确的单词)类中的对象是一个好主意,即:

public class Page
{
    public List<Paragraph> Paragraphs { get; set; }
}

public class Paragraph
{
    public List<QWord> Sentences { get; set; }
}

public class Sentence
{
    public List<QWord> Words { get; set; }
}

That'd help when you bind data in your XAML, ie if you look at the tutorial which Alex provided: 当您在XAML中绑定数据时,这会有所帮助,即如果您查看Alex提供的教程:

<TextBlock Text="Name: " />
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBlock Text="{Binding Age}" FontWeight="Bold" />
<TextBlock Text=" (" />
<TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
<TextBlock Text=")" />

Hope that helps you. 希望对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM