views:

161

answers:

3

Hello,

I'm building a simple financial record-keeping application. The main window view model holds a list of Accounts. The view shows this list (in a ListView) along with a panel showing details about the currently selected Account.

At first, I bound the details panel and ListView's SelectedItem to the same property (of type Account) on the view model. However, I quickly realized that the details panel needed to be bound to an AccountViewModel, not directly to an instance of Account.

There are several ways to providing this AccountViewModel:

  • Bind the details panel to a separate property on the view model. When ListView's SelectedItem changes, the view model should create and set this new property to an instance of AccountViewModel that is associated with the selected Account.
  • Give the main view model a list of AccountViewModels instead of an Accounts list. Both the ListView listing all accounts and the details panel could then be bound to the same property on the main view model.
  • Have one AccountViewModel, changing the Account it references with each change to ListView's SelectedItem property.

Are there other options? Which choice do you recommend?

Thank you, Ben

A: 

i like this option

  • Bind the details panel to a separate property on the view model. When ListView's SelectedItem changes, the view model should create and set this new property to an instance of AccountViewModel that is associated with the selected Account.

then you can bind the selected item to the view model and the details pane as well. this can be tested independently of the view. when you create your list of accounts you can also create a list of account view models, so when you change selection you are not having to create anything its all sitting there. this way your viewmodel is comprehensive, representing the whole screen not just floating bits of viewModel.

Aran Mulholland
+1  A: 

This is what I do:

Give the main view model a list of AccountViewModels instead of an Accounts list.

This will serve you well in many ways. I always find that eventually, for one reason or another, I need to augment my Models in some way to support a View, so these days I just start out by creating ViewModels.

The way you can tell that this is going to be a better option is that it involves less code. Less code always equals less bugs, in my opinion.

Anderson Imes
As I've thought about this approach, I've wondered: Shouldn't a view model correspond with a view? That is, if there is only one account view, should there only be one account vm or is it okay to have several? If we have multiple view models for one view, are we implementing something other than M-V-VM?
Ben Gribaudo
I think it's ok for a ViewModel to *have* other ViewModels as properties, if that's what you mean. I don't think that one view = one view model. If you mean that you might need more than one ViewModel to represent the same Model so as to allow for different views to show the same bits of data in different ways and with different behavior, you are correct. There are times when I've has more than one version of the "AccountViewModel" because different views need different behaviors.
Anderson Imes
Josh Smith uses a collection of ViewModels as a property in a ViewModel in his canonical MVVM sample application. (Commands of MainWindowViewModel).
Guge
@Guge: Exactly!
Anderson Imes
A: 

So far, the idea I like best is #3 from the original post for two reaons:

  • It has a 1 view to 1 view model correspondence, which I think best fits the M-V-VM pattern.
  • Since the account view model is told when to change to display details of a different account, it can ask the user if he wants unsaved edits to be saved before changing to display the new account (etc.).
Ben Gribaudo