In my application, I was generally using the ViewModel constructor without any parameter and locating my ViewModel from xaml like below.
<UserControl.Resources>
<ResourceDictionary>
<vm:TabViewModel x:Key ="vm" ></vm:TabViewModel>
</ResourceDictionary>
</UserControl.Resources>
By using this, I can reference my ViewModel properties easily at design time in the xaml.
<Grid x:Name="grid" DataContext="{Binding Source={StaticResource ResourceKey=vm}}">
However, since I would communicate between the two ViewModels, I started using Prism 6 (Event Aggregator).
public TabViewModel(IEventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;
tabPoco = new TabWindowPoco();
tabPoco.Tag = "tag";
tabPoco.Title = "Title";
tabPoco.ListDataList = new ObservableCollection<ListData>();
tabPoco.ListDataList.Add(new ListData() { Name = "First", Age = 10, Country = "Belgium" });
tabPoco.ListDataList.Add(new ListData() { Name = "Second", Age = 11, Country = "USA" });
tabPoco.ListDataList.Add(new ListData() { Name = "Third", Age = 12, Country = "France" });
}
I am using Bootstrapper to load the application.
Since I am using IEventAggregator, I am force to use prism:ViewModelLocator.AutoWireViewModel="True" to locate ViewModel. Otherwise, the application does not run. Now, I am not able to use ViewModel as resource and cannot attach it to DataContext.
I do not want Prism to automatically locate the respective ViewModel, I would like to have control over it. I would like to locate it in xaml and assign it to DataContext.
Does anyone know how do I achieve this?
This looks like you have failed to set up your dependency injection container. Dependency injection uses a preset mapping between interface types and concrete types. Your mapping will look similar to this (Prism/EntLib 5 example):
public class Bootstrapper : UnityBootstrapper
{
protected override void ConfigureContainer()
{
base.ConfigureContainer();
this.Container.RegisterType<IMyViewModel, SomeViewModel>();
this.Container.RegisterType<IFooViewModel, SomeOtherViewModel>();
}
}
Later when you make a call:
this.DataContext = container.Resolve<IMyViewModel>();
This is more powerful than this little example would suggest - you can new up a view from your container (or region manager) and end up resolving all its dependencies automatically.
To maintain intellisense etc in your XAML you can specify the type of your viewmodel in the XAML:
<UserControl x:Class="YourNamespace.YourFancyView"
...
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:myNamespaceAlias="clr-namespace:PathToMyViewModelsInterface"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="100"
d:DataContext="{d:DesignInstance myNamespaceAlias:IMyViewModel, IsDesignTimeCreatable=False}"
...
>
</UserControl>
Check the related SO question How do I specify DataContext (ViewModel) type to get design-time binding checking in XAML editor without creating a ViewModel object? and Using Design-time Databinding While Developing a WPF User Control for more information.