1

I am working on a winforms project in c#. The project is an outlook plugin, so no hopes of totally converting to mvvmlight/wpf as it's a winforms dll project.

I am however trying to come up with a way to use MvvM Light and WPF with the ElementHost. The issue I have is getting access to the ViewModelLocator. Normally, this gets added in the App.xaml like this:

<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

and is available to a view via :

DataContext="{Binding MyViewModel, Source={StaticResource Locator}}"

In my case, I don't have an App.xaml, nor do I have a program main() method where I can bootstrap the framework (as this is a dll project, not an application). I tried the following in my WPF usercontrol (hosted in an elementhost), but it doesn't work:

DataContext="{Binding MyViewModel, Source={StaticResource Locator}}"
...
<UserControl.Resources>        
    <ResourceDictionary>
        <wpf:ViewModelLocator x:Key="Locator"
                         d:IsDataSource="True" />

I think it's not working because it's getting declared after its called. This would have been a compromise anyway as I would have needed that code in every view, so I'm kind of glad it didn't work.

Any suggestions how I can get to the ViewModelLocator??

Thanks, Jeff

Jeff
  • 387
  • 1
  • 3
  • 11

1 Answers1

0

So I figured out what to do to solve this. It's not perfect, but it works. In the end, I have no Application to load the bootstrap, so what I really needed was a way to have a view get to the ViewModelLocator so I could databind the view. To do this I made the MyViewModel property static on the ViewModelLocator class and then I changed my DataContext line in the xaml (the view) as follows:

DataContext="{x:Static wpf:ViewModelLocator.MyViewModel}"
Jeff
  • 387
  • 1
  • 3
  • 11
  • Hi. I am struggling with the same issue now. When I use the line you wrote above, I got error. Can you please send the whole (or as much as possible) xaml code. Thanks! – Lati Dec 08 '17 at 15:10