Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding List of Lists in XAML?

Tags:

c#

wpf

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.

like image 670
Lucifer Avatar asked Nov 18 '11 19:11

Lucifer


1 Answers

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>
like image 191
sll Avatar answered Oct 23 '22 12:10

sll