Open view with existing viewmodel

Dec 7, 2010 at 3:39 PM

I have a view with a listbox filled with viewmodels, and i now want the open one of these viewmodels in the correct view for editing.

Now i have got this working for a new ViewModel, but editing has not been working yet.

For a new one


var workspaceData = new WorkspaceData(@"/ZebraDesktopClient;component/Images/imageIcon.png",
                  "DataView", null, "Data View", true);
            Mediator.Instance.NotifyColleagues("AddWorkSpace", workspaceData);

And the addworkspace part is processed by my mainwindowmodel

how can i call this AddWorkSpace for an exiting item, i tired with instead of null parameter to give the currentitem viewmodel along with the workspacedata, but i think i am missing a step somewhere.

This is cinchv2.

Dec 7, 2010 at 4:27 PM
Edited Dec 7, 2010 at 4:30 PM

I am not sure I understand the question, But i have a feeling you are asking to be able to get the current ViewModel in the list to edit, where the list was created using a List<WorkSpaceData> and then Views were created from that. I think all you would need is to get the created ViewModel data from the WorkSpaceData again, once the View is created and given a MEF created ViewModel DataContext, and if you can get the ViewModel that was selected, you can edit it in another workspace, you know by having another list of WorkSpaceData (with maybe just 1 item in it for the edit you want to do).

If that sounds like what you are after I gave out an answer the other day that may help you:


Or you could just use the Mediator to send message from selected ViewModel to MainViewModel to say "EditMeMessage" where you would send entire selected ViewModel as message


Dec 7, 2010 at 7:08 PM

I have a List<DataViewModel> that is listed in a view with a listbox thats bound to this list.

Lets say its model is called DataLoaderViewModel (pretty much as your WPF cinchv2 example, has a View with listbox data bound to the List<DataViewModel>)


In the DataLoaderViewMOdel is have an command reads the currentitem from the collectionview

I then want this dataviewmodel to be opened in a new workspace wit a DataView view and the currentitem as the DataView DataViewModel for editing.


I tried with

var workspaceData = new WorkspaceData(@"/ZebraDesktopClient;component/Images/imageIcon.png",
                  "DataView", currentDataViewModel, "Data View", true);
            Mediator.Instance.NotifyColleagues("AddWorkSpace", workspaceData);


Where the curretnDataViewModel is the currentitem from the ViewCollection.

Maybe i need a extra step in my mainwindowmodel to actually set the dataviewmodel as the model for the view , instead of a new one which it now seems to make every time i open a workspace.

I am pretty new to the mvvm/cinch model so it could easily be that i misunderstood how this works, and that i am trying to do so something out of the ordinary.

Dec 8, 2010 at 9:02 AM

        var dataViewModel = (DataViewModel) LoadedDataCV.CurrentItem;
            dataViewModel.CurrentViewMode = ViewMode.EditMode;
            var workspaceData = new WorkspaceData(@"/ZebraDesktopClient;component/Images/imageIcon.png",
                  "DataView", dataViewModel, "Data View", true);


And in the view i execute this a have a itemscontrol which is bound to the views (tabcontrolex) and that all works fine.

But it does not load dataViewModel but always creates a new dataViewModel for the view.


Dec 8, 2010 at 9:33 AM

That will not work. WorkSpaceData will ALWAYS create a NEW view and MEF in a new ViewModel. You need to follow the advice I suggested above or examine the workspaceData context data being passed in (the dataViewModel in your case) and use that to populate the newly created ViewModel. Workspacedata is about creating new workspaces, I think you maybe need to look at the article around this part a bit more. There is an example of how it work for passing contextual data using the workspaceData  in the MainWindowViewModel and also look at the ImageLoaderView/ImageLoaderViewModel that uses the contextual data provided to it.

In the ImageLoaderViewModel there is something like this:

private void ViewAwareStatusService_ViewLoaded()
            if (!Designer.IsInDesignMode)
                var view = viewAwareStatusService.View;
                IWorkSpaceAware workspaceData = (IWorkSpaceAware)view;
                DirectoryName = (String)workspaceData.WorkSpaceContextualData.DataValue;


That basically gets the contextual data from the View which was create via the WorkSpaceData.

If you want to create a new workspace you will need to populate the newly MEF created ViewModel with the previous values that are passed as context to the View via workspaceData  (as I say this would be the dataViewModel in your case), and in the DataViewModel you would need to check for the arrival of any contextual data which is done via the use of the IWorkSpaceAware interface on the View that you could use on your DataViewModel. Where if you were to create a WorkSpaceData like you do above you could then use the previous DataViewModel to populate the newly MEF created one, where the DataViewModel contextual data would be coming in on the IWorkspaceAware.WorkSPaceContextualData property. Again see the WPF demo app for an example of dealing with contextual data.

To be honest I think you need to reread article around this feature



Dec 8, 2010 at 1:47 PM

Ahh, well i did reread most of it today, and i was expecting such an answer .

I think i was confused by the concept that i have that i should reuse the dataviewmodels, but this does make sense.

Thank you for your quick responses all the time, i am really loving the cinch framework as i understand more and more of it :)

Was surprised how easy it was to create a to views bounded tabcontrol.


Guess what i did wrong was use the same viewmodels in my listing as in my editing view.

I think that they should be different cause you cant reuse them anyways there is no reason for them to be the same. As the listing also does not use all the information (as in properties ) that the dataviewmodel has.





Dec 8, 2010 at 2:21 PM

I dont think there is anything wrong with you using same ViewModel in list and edit, you just need to realise that they will never be the same ViewModel instances. As CinchV2 is all View 1st and MEFs ViewModel in on new View creation. The workspacedata objects simply allow for a given view to be created from a ViewModel in a view 1st manner, but you can get data from the newly created View using the IWorkspaceAware interface as I showed above.


I think you will be ok now