如何在代码中绑定嵌套对象或主从绑定?
我有一个三个嵌套类,即节目、季节和剧集,其中节目有季节,季节有剧集。
我想绑定两个列表框,以便第一个列表框列出季节,第二个列表框列出该季节的剧集。
我怎样才能做到这一点? 我更喜欢在代码中设置它,而不是在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:添加更多细节。
好的,假设您有一个 Show 对象。 这有一个季节的集合。 每个季节都有一系列剧集。 然后,您可以将整个控件的 DataContext 作为 Show 对象。
列表框到 Seasons 集合。
ItemsSource="{绑定季节}"
IsSynchronizedWithCurrentItem="True"
列表框到当前季节
剧集合集。
ItemsSource="{绑定
Seasons/Episodes}”。
假设您的 Window 的 DataContext 是 Show 对象,则 XAML 将是:
因此您的 UI 元素实际上并不需要名称。此外,将其转换为代码非常容易,而且您走在正确的道路上。您的代码的主要问题是您命名了列表框,而它们实际上并不需要它。
假设 Season 对象有一个名为 Episodes 的属性,它是 Episode 对象的集合,我认为它是:
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.
list box to the Seasons collection.
ItemsSource="{Binding Seasons}"
IsSynchronizedWithCurrentItem="True"
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:
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: