ZoltanE
ZoltanE

Reputation: 173

Databind and lists in lists

I've made an example project to show my problem. It can be downloaded in full (BookExample.zip) but here is the quick rundown:

I have the following data model:

Course
  Name
  Book
    Name
    Pages
      Note
      Number
      Lines
        Note
        Number

The actual data is a list of Course instances where each contains a list of Page instances ('Pages') where each one of those contains a list of Line type objects ('Lines'). My goal is to bind this structure to the following UI: BookExample02.png

After properly setting up the data context, changing course selection changes the related book text and the list of page notes. However I can not go deeper: I couldn't make it so the "Line notes" list changes based on the selection in the "Page notes" list.

The only reason the "Line notes" list is not empty is because in XAML I wrote this:

<ListBox Height="80" x:Name="listBox3" ItemsSource="{Binding Book.Pages[0].Lines}" />

So it always shows the first page item's Lines property. How could I replace that 0 with the selection index in the "Page notes" listbox?

Upvotes: 1

Views: 71

Answers (2)

Botz3000
Botz3000

Reputation: 39610

Try setting your binding to the selected item in your pages listbox:

<ListBox Height="80" x:Name="listBox3" ItemsSource="{Binding SelectedItem.Lines, ElementName=listBox2}" />

Upvotes: 1

Eirik
Eirik

Reputation: 4205

Change the ItemsSource binding to this:

ItemsSource="{Binding Path=SelectedItem.Lines, ElementName=listBox2}"

The note list will then update correctly as you select a page in the page list.

Upvotes: 1

Related Questions