I have a control on a page that contains a listbox. I also have another control which is a detail view.
Both of the controls have their own ViewModel which their child controls bind to.
Image Outlook. It has a list of folders 开发者_StackOverflow社区and when you select a folder the detail control displays the contents of the folder.
How can I bind the detail control to the selected item in the list control?
If I got it correctly, you have master-detail situation. Can you add list of details ViewModel as property of master ViewModel? That way you don't need anything special. It should work automatically. Something like this:
public class MyMasterViewModel
{
public List<MyDetailViewModel> Details
{ get; set; }
}
Set collection of MyMasterViewModel
as DataContext
to both views and configure binding appropriately. As you move through master list, detail list will be automatically updated.
You'll probably need to set IsSynchronizedWithCurrentItem
property:
<ListBox ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True"
DisplayMemberPath="Something"/>
Use element-to-element binding, here as link to MSDN page http://msdn.microsoft.com/en-us/library/ms752347.aspx with master-detail example.
精彩评论