开发者

Silverlight: Bind to all items in nested lists

开发者 https://www.devze.com 2022-12-19 02:50 出处:网络
I\'m using Silverlight 3 with RIA services. I\'ve got a simple RIA DomainDataSource named \"source\" with a couple of ListBoxes bound to it. The method that the source queries returns a simple graph o

I'm using Silverlight 3 with RIA services. I've got a simple RIA DomainDataSource named "source" with a couple of ListBoxes bound to it. The method that the source queries returns a simple graph of objects: a collection of Parents, where each parent has a collection of Children.

My UI has two listboxes. ParentListBox binds ItemsSource to {Binding Data, ElementName=source}", with a DisplayMemberPath of Name. This shows all the parents' names, which is what I want.

ChildrenListBox binds 开发者_JAVA百科ItemsSource to {Binding Data.Children, ElementName=source}. The result is that ChildrenListBox shows all the children of the parent currently selected in ParentListBox: when I change the selection, the values in the ChildrenListBox change. This is pretty smart behavior, I guess, but not what I want. What I want is for this second ListBox to display all of the children of all of the parents all the time, regardless of what is selected in ParentListBox. How can I achieve this?


Create a ChildrenList property on your view model with the IEnumerable of your choice. Bind the ChildrenListBox ItemsSource property to ChildrenList. In your web callback, populate the underlying collection for the ChildrenList property with the parents that were returned from the RIA service.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号