Silverlight:绑定到嵌套列表中的所有项目

时间:2010-02-05 23:12:22

标签: silverlight collections binding

我正在使用带有RIA服务的Silverlight 3。我有一个名为“source”的简单RIA DomainDataSource,它绑定了几个ListBox。源查询返回简单对象图的方法:父对象集合,其中每个父对象都有一个子集合。

我的用户界面有两个列表框。 ParentListBox将ItemsSource绑定到{Binding Data,ElementName = source}“,使用DisplayMemberPath of Name。这显示了所有父母的姓名,这就是我想要的。

ChildrenListBox将ItemsSource绑定到{Binding Data.Children,ElementName = source}。结果是ChildrenListBox显示当前在ParentListBox中选择的父项的所有子 :当我更改选择时,ChildrenListBox中的值会发生变化。我猜这是非常聪明的行为,但不是我想要的。我想要的是第二个ListBox始终显示父母所有子女的所有,无论在ParentListBox中选择什么。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

使用您选择的IEnumerable在视图模型上创建一个ChildrenList属性。 将ChildrenListBox ItemsSource属性绑定到ChildrenList。 在您的Web回调中,使用从RIA服务返回的父项填充ChildrenList属性的基础集合。

foreach(var parent in ParentList)
{
    foreach(var child in parent.Children)
    {
        _childrenList.Add(child)
    }
}