tags:

views:

47

answers:

2

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 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?

A: 

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"/>
zendar
My problem is that the master view is in a different control to the detail view. I will try your point about using the same ViewModel for both controls
David Ward
If they are on same window then you can set `DataContext` on window. If you have other data context for window, see if you can put those two views together in some container (grid or some of the layout panels) and then set `DataContext` to that container.
zendar
A: 

Use element-to-element binding, here as link to MSDN page http://msdn.microsoft.com/en-us/library/ms752347.aspx with master-detail example.

Karel Frajtak