Data Binding with WPF and MVVM/Model-View-ViewModel - wpf

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!

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.

Related

Is it possible the datagrid short the collection of the view model?

I have a DataGrid that binds to an ObservableCollection in the viewModel. I wanted to sort the items of the DataGrid by many columns, so I would like the ObservableCollection in the Viewmodel to have the items in the same order. Otherwise, using the SelectedIndex property in the ViewModel, it could select incorrect items.
To do it, I try to use the mode TwoWay in the view, but the collection is not sorted in the view model.
So I would like to know if it is possible to sync this.

Bind dynamic value coming from GUI to the viewmodel

There is an itemscontrol in one of my views that lets the user reorder the items by an up/down button.
My question is on how I can flow the current index of the items to my viewmodel
The following questions address on how to find the current index (not so clean but it does the job)
WPF - Bind to Item Index from within ItemTemplate of ItemsControl?
Now, how can I modify the XAML so that it binds this index to a property on my viewmodel?
edit The question is about how to do it in XAML declaratively. A possible solution is to do it in code using and ObservableCollection and subscribe to CollectionChanged
Kind Regards, Tom
So your items store an index poperty that you need to update when they are re-ordered in the UI?
I would create a view model that exposes my items as an ObservableCollection. You can then handle the CollectionChanged event which will fire when the items are re-ordered in your view model. At this point you can enumerate the collection of items updating their indices.

Master View implementation using MVVM in Silverlight

I have a silverlight datagrid to which I am binding an observable collection from the viewmodel. There is a detail view page which will display different properties of the object in the collection when user selects a row of the datagrid. My requirement is when user updates any properties in the detail view; the data should be updated in the data grid also. How to implement this functionality?
Well, the answer is simply to bind both the datagrid row and the control displaying the selected object. The simplest way is to use a ICollectionView (returned by a CollectionViewSource from the original ObservableCollection), bind the grid's ItemsSource to that, and then bind the control's DataContext to the ICollectionView's CurrentItem. That way, when the grid's selected item changes, the CurrentItem of the ICollectionView is updated, and that item is displayed in the detail view.
I think it's quite easy but if you need additional details or sample source code I'll elaborate.

How do I loop through a set of items in a databound Silverlight control such as the ListBox?

I have a silverlight ListBox that has it's ItemsSource set. From the C# code behind I want to loop through each ListBox item and change the value of the text and access controls that are in the ListBox ItemTemplate. How would I do that?
ListBox controls in silverlight are bound to an ienumerable type, so that if any value in the ListBox changes, the underlying data is changed and vice versa depending on what type of binding you require. To effectively iterate the items you'll want to iterate through the enumerable object you've bound to.
You can get to the collection by accessing the ListBox.ItemsSource property and change the text of appropriate items, perform LINQqueries etc. If you have bound the controls correctly, saving the collection should update the list.
Hope this helps!

Sharing state between ViewModels

I have two ViewModels that present the same Model to different Views. One presents the model as an item in a ListBox, the other presents it as a tab in a TabControl. The TabControl is to display tabs for the items that are selected in the ListBox, so that the tabs come and go as the selection changes.
I can easily synchronise the two controls by adding an IsSelected property to the Model and binding the ViewModels to it (a bit like this), but this will clutter the Model with presentation details that don't really belong there.
It seems like I need something between the Model and ViewModels to hold this extra state. Are there any patterns or examples of a good way to do this?
Use a ViewModel.
You've got a View that contains the two controls. Have a view model that will contain a list of ViewModels for the ListBox control to bind to. Also within this view model bind the listbox selection to a second list of viewmodels that the TabControl then also binds to.
That way your listbox drives what the tab control shows without this information entering the model which should stay oblivious to the existence of the view.
TabControl is ItemsControl, so you shouldn't be shy to bind its ItemsSource to ListBox.SelectedITems.
Obviously ViewModel for List should have a property that would produce ViewModel for Tabs:
public TabViewModel ItemTabModel { get { ... } }
And because TabControl is a bit funny, you'd need to add ItemContainerStyle to populate Content for TabControlItem, because the normal ItemTemplate for TableControl only affects headers for tabs.

Resources