简体   繁体   中英

Blendability for ViewModel instances in MVVM Light

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?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM