How to bind nested objects or master-detail-binding in code?

I have three nested classes: Show, Season and Episode, where the show has seasons and the seasons have episodes.

I want to link the two lists to the first list of seasons, and the second list of episodes this season.

How can i do this? I prefer to install this in code rather than in xaml, but if you know how to do it with xaml, this is better than nothing ..

Simplified xaml:

<Window>
  <Label name="Showname" />
  <ListBox name="Seasons" />
  <ListBox name="Episodes" />
</Window>

and some code:

public partial class Window1 : Window
{
  public Data.Show show { get; set; }
  public Window1()
  {
    this.DataContex = show;

    //Bind shows name to label
    Binding bindName = new Binding("Name");
    ShowName.SetBinding(Label.ContentProperty, bindName);

    //Bind shows seasons to first listbox
    Binding bindSeasons = new Binding("Seasons");
    Seasons.SetBinding(ListBox.ItemsSourceProperty, bindSeasons);
    Seasons.DisplayMemberPath = "SeasonNumber";
    Seasons.IsSyncronizedWithCurrentItem = true;

    //Bind current seasons episodes to second listbox
    Binding bindEpisodes = new Binding("?????");
    Episodes.SetBinding(ListBox.ItemsSourceProperty, bindEpisodes);
    Episodes.DisplayMemberPath = "EpisodeTitle";
  }
}

Has anyone figured out how to link the second list?

+3
source share
1 answer

Edit: Adding a bit more details.

, , Show. . . DataContext Show.

  • TextBlock . Text = "{Binding Name" }
  • ItemsSource " ". ItemsSource = "{Binding Seasons}" IsSynchronizedWithCurrentItem = "True"
  • ItemsSource . ItemsSource = "{Binding /}".

, Window DataContext Show, XAML :

<Window>
   <TextBlock Text="{Binding Name}" />
   <ListBox ItemsSource="{Binding Seasons}" IsSynchronizedWithCurrentItem="True" />
   <ListBox ItemsSource="{Binding Seasons/Episodes}" />   
</Window>

. , , . , , .

, Season Episodes, Episode, , :

 Binding bindEpisodes = new Binding("Seasons/Episodes");
+8

Source: https://habr.com/ru/post/1702660/


All Articles