3

Show、Season、Episode の 3 つの入れ子になったクラスがあり、show には季節があり、season にはエピソードがあります。

2 つのリストボックスをバインドして、最初のリストにシーズンを、2 番目にそのシーズンのエピソードをリストしたいと考えています。

これどうやってするの?私はこれを xaml ではなくコードで設定することを好みますが、xaml でそれを行う方法を知っていれば、何もないよりはましです..

単純化された xaml:

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

およびいくつかの関連コード:

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";
  }
}

2 番目のリストボックスをバインドする方法について手がかりを得た人はいますか?

4

1 に答える 1

8

編集:もう少し詳細を追加します。

では、Show オブジェクトがあるとしましょう。これには季節のコレクションがあります。各シーズンにはエピソードのコレクションがあります。その後、コントロール全体の DataContext を Show オブジェクトにすることができます。

  • TextBlock を番組の Name にバインドします。Text="{バインディング名"}
  • Seasons リスト ボックスの ItemsSource を Seasons コレクションにバインドします。ItemsSource="{Binding Seasons}" IsSynchronizedWithCurrentItem="True"
  • エピソード リスト ボックスの ItemsSource を現在のシーズンのエピソード コレクションにバインドします。ItemsSource="{バインド シーズン/エピソード}".

Window の DataContext が Show オブジェクトであると仮定すると、XAML は次のようになります。

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

したがって、UI 要素に名前は必要ありません。また、これをコードに変換するのは非常に簡単で、正しい道を進んでいます。コードの主な問題は、実際には必要ないリスト ボックスに名前を付けていたことです。

Season オブジェクトに Episodes オブジェクトのコレクションである Episodes というプロパティがあると仮定すると、次のようになると思います。

 Binding bindEpisodes = new Binding("Seasons/Episodes");
于 2009-02-03T21:20:55.813 に答える