Myself and my colleague were having a discussion about MVVM and how lists and selected items should be stored in view / viewmodel.
I think the viewmodel should only offer up the data and allow the view to interpret it how it likes. It could display it as a list with a selected item or it could show it completely differently.
Therefore I think the viewmodel should look like this:
public class MajorVM
{
ObservableCollection<MinorVM> List
}
And the view like this:
<Grid>
<Listbox Key=ListBox Items=Binding DataContext.List />
<ContentControl Content=Binding ListBox.SelectedItem />
</Grid>
My colleague thinks that I'm changing between data contexts making the view code harder to follow.
My colleague believes the viewmodel should contain the state of the view and that this will allow easier manipulation of the view in future. E.g. controlling selection via the viewmodel.
Therefore it would look like:
public class MajorVM
{
ObservableCollection<MinorVM> List
MinorVM SelectedItem
}
With a view that looks like this:
<Grid>
<Listbox Items=Binding DataContext.List />
<ContentControl Content=Binding DataContext.SelectedItem />
</Grid>
To me this doesn't seem correct because the viewmodel now has a SelectedItem property on it that isn't mentioned anywhere other than the view. It's a view implementation on the viewmodel.