1

I've used MVVM before, but MVVM-Light confuses the hell out of me, so I'm sorry if this is dumb (it's just that there's barely any documentation).

Anyways, I've learnt that you use the ViewModelLocator only for "singleton" views from Laurent's answer here.

So how do I handle view-models that aren't in the locator? because it seems like I'm losing the benefits MVVM-Light has there.

What I do now is go to the view's code-behind and create a dependency property for my view-model and set my data-context to it.

Is there a better way? because I get no blendability whatsoever (Visual Studio and ReSharper doesn't even recognize the data context in the XAML editor). Not to mention I have no dummy data to design against.

In other words:
From what I've seen the blendability comes from a dependency injection found the locator, so what do you do when you're not using the locator?

Community
  • 1
  • 1
MasterMastic
  • 20,711
  • 12
  • 68
  • 90
  • AFAIK you can get a new VM instance by passing a unique key while resolving the VM from `ServiceLocator`. You're not tied into having to use the VMLocator for just singleton's that way. – Viv Aug 06 '13 at 13:23
  • 1
    [This](http://stackoverflow.com/a/16994523/1834662) shows an example of requesting new VM's based on a uniqueKey and disposing them when the View is closed. Check MainWindow code-behind part. As for `how could I bind a view-model instance to the view (that's not in the locator)` not sure if I follow this right, but you could just set `DataContext = new MyDummyVM();` in the View's code-behind constructor or if you want it to be for design time alone, with MVVM light you could do `if (ViewModelBase.IsInDesignModeStatic) DataContext = new MyDummyVM();` – Viv Aug 06 '13 at 13:34
  • @Viv I think I've got it! you're more than welcomed to post this as an answer to get the rep. I'll test & try putting this information to use right now. – MasterMastic Aug 06 '13 at 13:36

1 Answers1

1

Converting my comment to an answer

AFAIK you can get a new VM instance by passing a unique key while resolving the VM from ServiceLocator. You're not tied into having to use the VMLocator for just singleton's that way.

You can get an example of this procedure from Here. In MainWindow.xaml.cs when a new non-modal Window is requested, each instance of the view is coupled with a new instance of the corresponding VM, which can be found in the code-behind.

how could I bind a view-model instance to the view (that's not in the locator)

^^ Not really sure if this is what you're after, however with MVVM Light(for desgin time VM), you can just set DataContext of the View in it's constructor after you check if in design mode

something like:

using GalaSoft.MvvmLight;
...

public MainWindow() {
  InitializeComponent();
  if (ViewModelBase.IsInDesignModeStatic)
    DataContext = new MainViewModel();
}
Community
  • 1
  • 1
Viv
  • 17,170
  • 4
  • 51
  • 71
  • Terrific! thank you again. What I meant is how do I bind an existing view-model to a view if the view gets the view-model from the locator. But I was dumb & didn't think it through & then realized "Just set the data context". I've tried it now and it works like a charm! – MasterMastic Aug 06 '13 at 14:25