Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

c# - Binding List of Lists in XAML?

I have a List of Lists object:

List<List<Movie>> MovieList

This MovieList object is a collection of lists of movies, each based on a particular movie genre. ex. MovieList[0] will be a list of movies of Comedy genre, and so on Now I want to bind this List of Lists MovieList object to a ListView in XAML. The ListView ItemSource is to be bound to this MovieList object and each ListViewItem of this ListView will be a ListView itself, bound to the list of movies of a particular genre. ex. list of movies of comedy genre. Further each ListViewItem of this inner List will be bound to the Title property of that particular movie. Please help me out in designing the XAML Code for this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

MVVM solution:

MainWindow:

var moviesView = new MoviesView();
moviesView.DataContext = new MoviesViewModel { MovieList = ... };

MoviesViewModel.cs:

public class MoviesViewModel
{
    public ObservableCollection<List<Movie>> MovieList 
    { 
        get; 
        set; 
    }
}

MoviesView.xaml

<ListView ItemsSource="{Binding MovieList}">
   <ListView.ItemTemplate>
      <DataTemplate>
            <ListView ItemsSource="{Binding}">
               <ListView.ItemTemplate>
                   <DataTemplate>
                         <TextBlock Text="{Binding Title}" />
                   </DataTemplate>
               </ListView.ItemTemplate>
            </ListView>
      </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...