views:

179

answers:

1

I have an application which has a similar interface to Visual Studio, in that there's a list of documents that can be opened, edited an saved. Each document can be of different types and has different editors.

I also have a general Save MenuItem. What I want to do is have the Save command only save the active document. Is there a standard pattern using MVVM of connecting the documents VM to the Save MenuItem?

+1  A: 

I don't know about a standard way, but I would try something like the following:

  1. Create an interface that has a stub for your SaveDocument command.
  2. Have your document's ViewModel's implement that interface.
  3. Have your main app that has the list of current documents have a property that returns the viewmodel document the user is currently focused on. Call it CurrentDocument for arguement's sake. Let WPF databinding set this when they switch from doc to doc.
  4. Have your save command use the CurrentDocument property to get the document. check that it isn't null and implements the appropriate interface. If all checks pass, call the SaveDocument command for the ViewModel doc.
RB Davidson
Yep. That's what I ended up doing since I didn't get a response. +1
Cameron MacFarland
The only difference is the menus viewmodel has a save command I bind to, so that if there is no savable document open then the default save command is used (which does nothing.) When the document changes I set the menu viewmodel command to the savable documents command, or the default.
Cameron MacFarland