如何在代码中绑定嵌套对象或主从绑定?

发布于 2024-07-13 12:39:51 字数 1162 浏览 6 评论 0原文

我有一个三个嵌套类,即节目、季节和剧集,其中节目有季节,季节有剧集。

我想绑定两个列表框,以便第一个列表框列出季节,第二个列表框列出该季节的剧集。

我怎样才能做到这一点? 我更喜欢在代码中设置它,而不是在 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";
  }
}

有人知道如何绑定第二个列表框吗?

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

I want to bind two listboxes so that the first lists seasons, and the second lists episodes in that season.

How can I do this? I prefer to set this up in code, not xaml, but if you know how to do it with xaml, it's better than nothing..

A simplifyed xaml:

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

and some relevant 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";
  }
}

Anyone got any clues how to bind up the second listbox?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我老 2024-07-20 12:39:51

编辑:添加更多细节。

好的,假设您有一个 Show 对象。 这有一个季节的集合。 每个季节都有一系列剧集。 然后,您可以将整个控件的 DataContext 作为 Show 对象。

  • 将您的 TextBlock 绑定到节目的名称。 Text="{Binding Name"}
  • 绑定季节的ItemsSource
    列表框到 Seasons 集合。
    ItemsSource="{绑定季节}"
    IsSynchronizedWithCurrentItem="True"
  • 绑定剧集的 ItemsSource
    列表框到当前季节
    剧集合集。
    ItemsSource="{绑定
    Seasons/Episodes}”。

假设您的 Window 的 DataContext 是 Show 对象,则 XAML 将是:

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

因此您的 UI 元素实际上并不需要名称。此外,将其转换为代码非常容易,而且您走在正确的道路上。您的代码的主要问题是您命名了列表框,而它们实际上并不需要它。

假设 Season 对象有一个名为 Episodes 的属性,它是 Episode 对象的集合,我认为它是:

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

Edit: adding a bit more details.

Ok, so let's say you have a Show object. This has a collection of Seasons. Each Season has a collection of Episodes. You can then have the DataContext for the entire control be the Show object.

  • Bind your TextBlock to the show's Name. Text="{Binding Name"}
  • Bind the ItemsSource of the seasons
    list box to the Seasons collection.
    ItemsSource="{Binding Seasons}"
    IsSynchronizedWithCurrentItem="True"
  • Bind the ItemsSource of the episodes
    list box to the current Season's
    Episodes collection.
    ItemsSource="{Binding
    Seasons/Episodes}".

Assuming your Window's DataContext is the Show object, the XAML would be:

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

So your UI elements don't really need names. Also, to translate this into code is pretty easy and you were on the right path. The main issue with your code was that you were naming the list boxes, when they don't really need it.

Assuming the Season object has a property called Episodes, which is a collection of Episode objects, I think it is:

 Binding bindEpisodes = new Binding("Seasons/Episodes");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文