views:

327

answers:

1

I'm having difficulty correctly Data Binding my models in the view. I have a MainWindowViewModel which contains a list of AlbumViewModels. Each AlbumViewModel holds an AlbumModel. So I have multiple albums and I need to display information in an ObservableCollection in the AlbumModel. I have two ListBoxes. ListBox1 holds the list of AlbumViewModels that are in my MainWindowViewModel. My second ListBox I want to display the ObservableCollection from the current selected item from the AlbumViewModel.AlbumModel. How can I do this? I've tried binding the DataContext of ListBox2 to the ListBox1 element, along with SelectedItem as the path but that returns 'AlbumViewModel'. Is there anyway to bind ItemsSource of a ListBox to the binding of the DataContext, but in this case binding it to [DataContext].AlbumModel.ObservableCollection or something?

I apologise if it sounds rather complicated!

A: 

You can use the fact, that when you bind to a collection, WPF wraps collection to CollectionView. And this guy has CurrentItem.. Bea had good article: How can I sync selection of two data bound ListBoxes? and Dr.WPF is amazing (as usual): ItemsControl: 'C' is for Collection.

Anvaka
Ah ok, that's interesting. Thanks. It's worked but now I'm trying to figure out how to display my Model. Obviously I don't want the View to have access to the Model, so how can I create a DateTemplate for it without exposing the Model?edit: Nevermind. it's working. I just created a DataTemplate with a key rather than a DataType :) Thanks muchly!
Harry
You are welcome :). Glad I could help. Cheers
Anvaka